Hantera cellkontroller i arbetsblad

Åtkomst till cellkontroller

För att komma åt och ändra en befintlig cellkontroll i arbetsbladet kan utvecklare komma åt en specifik cellkontroll från Controls -samlingen av Worksheet genom att ange cellen (använda cellnamnet eller dess plats i form av rad- och kolumnnummer) där cellkontrollen visas. När en cellkontroll har kommit åt kan utvecklare ändra dess egenskaper under körning. Till exempel, i det givna exemplet nedan har vi kommit åt en befintlig CheckBox cellkontroll från Worksheet och ändrat dess Checked-egenskap.

Viktigt: Controls -samlingen innehåller alla typer av cellkontroller i form av CellControl -objekt. Så om du behöver komma åt en specifik typ av cellkontroll, säg CheckBox måste du typomvandla CellControl -objektet till CheckBox -klass.

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

Ta bort cellkontroller

För att ta bort en befintlig cellkontroll kan utvecklare helt enkelt komma åt ett önskat arbetsblad och sedan Ta bort cellkontrollen från Controls -samlingen av Worksheet genom att ange cellen (använda dess namn eller rad- och kolumnnummer) som innehåller cellkontrollen.

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