管理工作表中的单元格控件

访问单元格控件

要访问和修改工作表中现有的单元格控件,开发人员可以通过指定单元格(使用单元格名称或其在行和列编号方面的位置)从WorksheetControls集合中访问特定的单元格控件。一旦访问了单元格控件,开发人员可以在运行时修改它的属性。例如,在下面的示例中,我们从Worksheet中访问了一个现有的CheckBox单元格控件,并修改了它的Checked属性。

重要: Controls集合以CellControl对象的形式包含所有类型的单元格控件。因此,如果您需要访问特定类型的单元格控件,比如CheckBox,则需要将CellControl对象强制转换为CheckBox类。

// 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();
// Getting the location of the cell that is currently in focus
CellLocation cl = sheet.GetFocusedCellLocation();
// Accessing cell control and typecasting it to CheckBox
Aspose.Cells.GridDesktop.CheckBox cb = (Aspose.Cells.GridDesktop.CheckBox)sheet.Controls[cl.Row, cl.Column];
if (cb != null)
{
// Modifying the Checked property of CheckBox
cb.Checked = true;
}
else
{
MessageBox.Show("Please add control before accessing it.");
}

删除单元格控件

要删除现有单元格控件,开发人员只需访问所需的工作表,然后通过指定包含单元格控件的Controls集合中的单元格(使用其名称或行和列编号)来删除单元格控件。

// 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();
// Getting the location of the cell that is currently in focus
CellLocation cl = sheet.GetFocusedCellLocation();
// Removing the cell control by specifying the location of cell containing it
sheet.Controls.Remove(cl.Row, cl.Column);