Lägger till Cell kontroller i kalkylblad

Introduktion

För närvarande stöder Aspose.Cells.GridDesktop att lägga till tre typer av cellkontroller, som inkluderar följande:

  • Knapp
  • Kryssruta
  • Kombinationsrutan

Alla dessa kontroller är härledda från en abstrakt klass,CellControlVarje arbetsblad innehåller en samling avKontroller. Nya cellkontroller kan läggas till och befintliga kan nås med dettaKontrollerinsamling enkelt.

**VIKTIG:**Om du vill lägga till cellkontroller till alla celler i en kolumn istället för att lägga till en efter en kan du referera tillHantera Cell kontroller i kolumner.

Lägga till knapp

För att lägga till en knapp i kalkylbladet med Aspose.Cells.GridDesktop, följ stegen nedan:

  • Lägg till Aspose.Cells.GridDesktop-kontroll till dinForm
  • Få åtkomst till alla önskadeArbetsblad
  • Lägg tillKnapptillKontrollersamling avArbetsblad
// 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");

Medan du lägger tillKnapp, kan vi ange cellens plats (var den ska visas), bredd och höjd och rubriken för knappen.

Händelsehantering av knappen

Vi har diskuterat om att lägga tillKnappkontroll tillArbetsbladmen vad är fördelen med att bara ha en knapp i kalkylbladet om vi inte kan använda den. Så här kommer behovet av händelsehantering av knappen.

Att hanteraKlickhändelse avKnappkontroll, Aspose.Cells.GridDesktop gerCellButtonClickhändelse som bör genomföras av utvecklarna enligt deras behov. Till exempel har vi precis visat ett meddelande när knappen klickas som visas nedan:

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

Ange en bakgrundsbild för knappkontrollen

Vi kan ställa in bakgrundsbild/bild för knappkontrollen med dess etikett/text som visas i koden nedan:

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

VIKTIG:Alla händelser av cellkontroller innehåller enCellControlEventArgsargument som ger rad- och kolumnnumren för cellen som innehåller cellkontrollen (vars händelse utlöses).

Lägger till kryssruta

För att lägga till en kryssruta i kalkylbladet med Aspose.Cells.GridDesktop, följ stegen nedan:

  • Lägg till Aspose.Cells.GridDesktop-kontroll till dinForm
  • Få åtkomst till alla önskadeArbetsblad
  • Lägg tillKryssrutatillKontrollersamling avArbetsblad
// 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);

Medan du lägger tillKryssruta, kan vi ange cellens plats (var den ska visas) och status för kryssrutan.

Händelsehantering av CheckBox

Aspose.Cells.GridDesktop tillhandahållerCellCheckedChangedhändelse som utlöses närKontrolleradestatus för kryssrutan ändras. Utvecklare kan hantera denna händelse enligt deras krav. Till exempel har vi precis visat ett meddelande för att visaKontrolleradestatus för kryssrutan i koden nedan:

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

Lägger till ComboBox

För att lägga till en kombinationsruta i kalkylbladet med Aspose.Cells.GridDesktop, följ stegen nedan:

  • Lägg till Aspose.Cells.GridDesktop-kontroll till dinForm
  • Få åtkomst till alla önskadeArbetsblad
  • Skapa en uppsättning objekt (eller värden) som kommer att läggas tillKombinationsrutan
  • Lägg tillKombinationsrutantillKontrollersamling avArbetsbladgenom att ange platsen för cellen (där kombinationsrutan kommer att visas) och objekten/värdena som kommer att visas när kombinationsrutan ska klickas
// 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);

Händelsehantering av ComboBox

Aspose.Cells.GridDesktop tillhandahållerCellSelectedIndexChangedhändelse som utlöses närValt indexav kombinationsrutan ändras. Utvecklare kan hantera denna händelse enligt deras önskemål. Till exempel har vi precis visat ett meddelande för att visaValt objektav kombinationsrutan:

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