在工作表中添加 Cell 控件

介绍

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

  • 按钮
  • 复选框
  • 组合框

所有这些控件都派生自一个抽象类,细胞控制.每个工作表包含一个集合控件.可以添加新的单元格控件,并且可以使用此访问现有的单元格控件控件轻松收藏。

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

添加按钮

要使用 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提供单元格按钮点击开发人员应根据需要实施的事件。例如,我们刚刚在单击按钮时显示了一条消息,如下所示:

// 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);

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

CheckBox 的事件处理

Aspose.Cells.GridDesktop提供CellCheckedChanged勾选复选框的状态已更改。开发者可以根据自己的需求来处理这个事件。例如,我们刚刚显示了一条消息以显示勾选以下代码中复选框的状态:

// 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);

ComboBox的事件处理

Aspose.Cells.GridDesktop提供CellSelectedIndexChanged精选指数组合框的更改。开发者可以根据自己的意愿来处理这个事件。例如,我们刚刚显示了一条消息以显示所选项目组合框的:

// 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());
}