Introduction
Cell controls are infact those controls that can be added to worksheets. We call them Cell Controls because these controls are displayed in cells. In this topic, we will discuss about adding and handling the events of these cell controls.
Adding Cell Controls in Worksheets
Currently, Aspose.Grid.Desktop support adding three types of cell controls, which include the following:
All of these controls are derived from an abstract class, CellControl. Each worksheet contains a collection of Controls. New cell controls can be added and existing ones can be accessed using this Controls collection easily.
IMPORTANT: If you want to add cell controls to all cells of a column instead of adding one by one then you can refer to Managing Cell Controls in Columns.
Adding Button
To add a button into the worksheet using Aspose.Grid.Desktop, please follow the steps below:
- Add Aspose.Grid.Desktop control to your Form
- Access any desired Worksheet
- Add Button to the Controls collection of the Worksheet
NOTE: While adding Button, we can specify the cell's location (where to display it), width & height and the caption of the button.
Example:
[C#]
//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
sheet.Controls.AddButton(cl.Row, cl.Column, 80, 20, "Button");
[VB.NET]
'Accessing the worksheet of the Grid that is currently active
Dim sheet As Worksheet = gridDesktop1.GetActiveWorksheet()
'Accessing the location of the cell that is currently in focus
Dim cl As CellLocation = sheet.GetFocusedCellLocation()
'Adding button to the Controls collection of the Worksheet
sheet.Controls.AddButton(cl.Row, cl.Column, 80, 20, "Button")
We used the above code inside the Click event of a button and then selected B5 cell after running the application. After selecting the cell, we clicked on the button as shown below in the figure:
|
Figure: Clicking Add Button after selecting B5 cell
|
After the button is clicked, code is executed that added the Button control to B5 cell as shown below:
|
Figure: A Button added to B5 cell
|
Event Handling of Button
We have discussed about adding Button control to the Worksheet but what is the advantage of just having a button in the worksheet if we cannot use it. So, here comes the need of event handling of the button.
To handle the Click event of the Button control, Aspose.Grid.Desktop provides CellButtonClick event that should be implemented by the developers according to their needs. For an instance, we have just displayed a message when the button is clicked as shown below:
|
Figure: MessageBox popped up after clicking button
|
Example:
[C#]
//Implenting CellButtonClick event handler
private void gridDesktop1_CellButtonClick(object sender, Aspose.Grid.Desktop.CellControlEventArgs e)
{
//Displaying the message when button is clicked
MessageBox.Show("Button is clicked.");
}
[VB.NET]
'Implenting CellButtonClick event handler
Private Sub gridDesktop1_CellButtonClick(ByVal sender As Object,
ByVal e As Aspose.Grid.Desktop.CellControlEventArgs)
Handles gridDesktop1.CellButtonClick
'Displaying the message when button is clicked
MessageBox.Show("Button is clicked.")
End Sub
IMPORTANT: All events of cell controls contain a CellControlEventArgs argument that provides the row and column numbers of the cell that contains the cell control (whose event is triggered).
Adding CheckBox
To add a checkbox into the worksheet using Aspose.Grid.Desktop, please follow the steps below:
- Add Aspose.Grid.Desktop control to your Form
- Access any desired Worksheet
- Add CheckBox to the Controls collection of the Worksheet
NOTE: While adding CheckBox, we can specify the cell's location (where to display it) and state of the checkbox.
Example:
[C#]
//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);
[VB.NET]
'Accessing the worksheet of the Grid that is currently active
Dim sheet As Worksheet = gridDesktop1.GetActiveWorksheet()
'Accessing the location of the cell that is currently in focus
Dim cl As CellLocation = sheet.GetFocusedCellLocation()
'Adding checkbox to the Controls collection of the Worksheet
sheet.Controls.AddCheckBox(cl.Row, cl.Column, True)
The output of above code snippet is displayed in the figure shown below:
|
Figure: A CheckBox added to B5 cell
|
Event Handling of CheckBox
Aspose.Grid.Desktop provides CellCheckedChanged event that is triggered when the Checked state of the checkbox is changed. Developers can handle this event according to their requirements. For an instance, we have just displayed a message to show the Checked state of the checkbox:
|
Figure: MessageBox shown when state of CheckBox is changed
|
Example:
[C#]
//Implenting CellCheckedChanged event handler
private void gridDesktop1_CellCheckedChanged(object sender, Aspose.Grid.Desktop.CellControlEventArgs e)
{
//Getting the reference of the CheckBox control whose event is triggered
Aspose.Grid.Desktop.CheckBox check=
(Aspose.Grid.Desktop.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);
}
[VB.NET]
'Implenting CellCheckedChanged event handler
Private Sub gridDesktop1_CellCheckedChanged(ByVal sender As Object,
ByVal e As Aspose.Grid.Desktop.CellControlEventArgs)
Handles gridDesktop1.CellCheckedChanged
'Getting the reference of the CheckBox control whose event is triggered
Dim check As Aspose.Grid.Desktop.CheckBox = CType(gridDesktop1.GetActiveWorksheet().Controls(e.Row,e.Column),
Aspose.Grid.Desktop.CheckBox)
'Displaying the message when the Checked state of CheckBox is changed
MessageBox.Show("Current state of CheckBox is "+ check.Checked)
End Sub
Adding ComboBox
To add a combobox into the worksheet using Aspose.Grid.Desktop, please follow the steps below:
- Add Aspose.Grid.Desktop control to your Form
- Access any desired Worksheet
- Create an array of items (or values) that will be added to ComboBox
- Add ComboBox to the Controls collection of the Worksheet by specifying the location of cell (where combobox will be displayed) and the items/values that will be displayed when the combobox will be clicked
Example:
[C#]
//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);
[VB.NET]
'Accessing the worksheet of the Grid that is currently active
Dim sheet As Worksheet = gridDesktop1.GetActiveWorksheet()
'Accessing the location of the cell that is currently in focus
Dim cl As CellLocation = sheet.GetFocusedCellLocation()
'Creating an array of items or values that will be added to combobox
Dim items() As String = 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)
|
Figure: A ComboBox added to B5 cell
|
Event Handling of ComboBox
Aspose.Grid.Desktop provides CellSelectedIndexChanged event that is triggered when the Selected Index of combobox is changed. Developers can handle this event according to their desires. For an instance, we have just displayed a message to show the Selected Item of the combobox:
|
Figure: MessageBox showing selected item of ComboBox
|
Example:
[C#]
//Implenting CellSelectedIndexChanged event handler
private void gridDesktop1_CellSelectedIndexChanged(object sender, Aspose.Grid.Desktop.CellControlEventArgs e)
{
//Getting the reference of the ComboBox control whose event is triggered
Aspose.Grid.Desktop.ComboBox combo=
(Aspose.Grid.Desktop.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());
}
[VB.NET]
'Implenting CellSelectedIndexChanged event handler
Private Sub gridDesktop1_CellSelectedIndexChanged(ByVal sender As Object,
ByVal e As Aspose.Grid.Desktop.CellControlEventArgs)
Handles gridDesktop1.CellSelectedIndexChanged
'Getting the reference of the ComboBox control whose event is triggered
Dim combo As Aspose.Grid.Desktop.ComboBox = CType(gridDesktop1.GetActiveWorksheet().Controls(e.Row,e.Column),
Aspose.Grid.Desktop.ComboBox)
'Displaying the message when the Selected Index of ComboBox is changed
MessageBox.Show(combo.Items(combo.SelectedIndex).ToString())
End Sub
Describes how to manage cell controls in columns.
7/26/2006 6:51:24 PM - -210.56.19.13