在工作表中添加单元格控件

介绍

目前,Aspose.Cells.GridDesktop支持添加三种类型的单元格控件,包括如下:

  • 按钮
  • 复选框
  • 组合框

所有这些控件都衍生自一个抽象类CellControl。每个工作表都包含一个Controls集合。可以轻松地通过这个Controls集合添加新的单元格控件并访问现有的控件。

重要: 如果你想在一列的所有单元格中添加单元格控件,而不是一个一个添加,那么你可以参考在列中管理单元格控件。

添加按钮

使用Aspose.Cells.GridDesktop在工作表中添加按钮,请按照以下步骤操作:

  • 将Aspose.Cells.GridDesktop控件添加到您的表单
  • 访问任何所需的工作表
  • 按钮添加到工作表控件集合中
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
Worksheet sheet = gridDesktop1.GetActiveWorksheet();
// Accessing the location of the cell that is currently in focus
CellLocation cl = sheet.GetFocusedCellLocation();
// Adding button to the Controls collection of the Worksheet
Aspose.Cells.GridDesktop.Button button = sheet.Controls.AddButton(cl.Row, cl.Column, 80, 20, "Button");

在添加按钮时,我们可以指定单元格的位置(显示位置)、宽度和高度以及按钮的标题。

按钮的事件处理

我们已经讨论了在工作表中添加按钮控件,但是如果我们不能使用它,那么在工作表中只有一个按钮有什么优势呢?这就需要处理按钮的事件。

为了处理按钮控件的单击事件,Aspose.Cells.GridDesktop提供了CellButtonClick事件,开发人员应根据需要实现它。例如,当按钮被点击时,我们只显示一个消息,如下所示:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Implenting CellButtonClick event handler
private void gridDesktop1_CellButtonClick(object sender, CellControlEventArgs e)
{
// Displaying the message when button is clicked
MessageBox.Show("Button is clicked.");
}

为按钮控件指定背景图片

我们可以为按钮控件设置背景图片/图片,并附上标签/文本,如下所示的代码:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Set the image.
Image image = Image.FromFile(dataDir + @"AsposeLogo.jpg");
button.Image = image;

重要: 所有单元格控件的事件都包含一个CellControlEventArgs参数,该参数提供包含触发事件的单元格控件的行号和列号。

添加复选框

使用Aspose.Cells.GridDesktop在工作表中添加复选框,请按照以下步骤操作:

  • 将Aspose.Cells.GridDesktop控件添加到您的表单
  • 访问任何所需的工作表
  • 复选框添加到工作表控件集合中
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
Worksheet sheet = gridDesktop1.GetActiveWorksheet();
// Accessing the location of the cell that is currently in focus
CellLocation cl = sheet.GetFocusedCellLocation();
// Adding checkbox to the Controls collection of the Worksheet
sheet.Controls.AddCheckBox(cl.Row, cl.Column, true);

在添加复选框时,我们可以指定单元格的位置(显示位置)和复选框的状态。

复选框的事件处理

Aspose.Cells.GridDesktop提供了CellCheckedChanged事件,当复选框的Checked状态改变时触发。开发人员可以根据自己的需求处理此事件。例如,我们在下面的代码中只是显示了一个消息来显示复选框的Checked状态:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Implenting CellCheckedChanged event handler
private void gridDesktop1_CellCheckedChanged(object sender, CellControlEventArgs e)
{
// Getting the reference of the CheckBox control whose event is triggered
Aspose.Cells.GridDesktop.CheckBox check = (Aspose.Cells.GridDesktop.CheckBox)gridDesktop1.GetActiveWorksheet().Controls[e.Row, e.Column];
// Displaying the message when the Checked state of CheckBox is changed
MessageBox.Show("Current state of CheckBox is " + check.Checked);
}

添加下拉列表框

要使用Aspose.Cells.GridDesktop在工作表中添加下拉列表框,请按照以下步骤进行:

  • 将Aspose.Cells.GridDesktop控件添加到您的表单
  • 访问任何所需的工作表
  • 创建一个数组(或值)的集合,将被添加到下拉列表框
  • 通过指定单元格的位置(下拉列表框将显示的位置)和下拉列表框被点击时将显示的项目/值,将下拉列表框添加到工作表控件集合中
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Accessing the worksheet of the Grid that is currently active
Worksheet sheet = gridDesktop1.GetActiveWorksheet();
// Accessing the location of the cell that is currently in focus
CellLocation cl = sheet.GetFocusedCellLocation();
// Creating an array of items or values that will be added to combobox
string[] items = new string[3];
items[0] = "Aspose";
items[1] = "Aspose.Grid";
items[2] = "Aspose.Grid.Desktop";
// Adding combobox to the Controls collection of the Worksheet
sheet.Controls.AddComboBox(cl.Row, cl.Column, items);

下拉列表框的事件处理

Aspose.Cells.GridDesktop提供了CellSelectedIndexChanged事件,当下拉列表框的Selected Index改变时触发。开发人员可以根据自己的需要处理此事件。例如,我们只是显示一个消息来显示下拉列表框的Selected Item:

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Implenting CellSelectedIndexChanged event handler
private void gridDesktop1_CellSelectedIndexChanged(object sender, CellComboBoxEventArgs e)
{
// Getting the reference of the ComboBox control whose event is triggered
Aspose.Cells.GridDesktop.ComboBox combo =
(Aspose.Cells.GridDesktop.ComboBox)gridDesktop1.GetActiveWorksheet().Controls[e.Row, e.Column];
// Displaying the message when the Selected Index of ComboBox is changed
MessageBox.Show(combo.Items[combo.SelectedIndex].ToString());
}