[]
Form controls are objects which can be added to a worksheet to enable interaction with a cell or a data range available in the worksheet. You can seek input from the end-user or provide him with options to choose from by using these form controls. Hence, these controls are apt to create forms such as feedback forms or consent forms.
GcExcel provides nine form controls through Grapecity.Documents.Excel.Forms namespace which contains classes and interfaces for each supported form control.
The table below lists the supported form controls and their images.
Form Control | Snapshots |
---|---|
Button | |
Dropdown | |
Checkbox | |
Spinner | |
Listbox | |
Option button | |
Group box | |
Label | |
Scrollbar |
All the form controls possess some common features which are provided by IControl interface of the Grapecity.Documents.Excel.Forms namespace. You can disable these controls by setting the Enabled property to false, so that user cannot bring focus to that control. There is an option to even lock the controls from accepting user input by setting the Locked property to true. To define how a control is attached to the underlying cells, you can use the Placement property. You can also change ZOrder of the controls, bring form controls to front or send them to back by using the BringToFront and SendToBack properties.
GcExcel allows you to add or remove the form controls to a worksheet by using Controls property of the IWorksheet interface. To add a form control to worksheet, you can use Add method of the IControlCollection interface. For instance, AddButton method adds the button form control and AddDropdown method adds the dropdown control to worksheet. So, there are nine such methods, one for each form control and all of them accept location coordinates, width, and height of the form control as parameters.
GcExcel provides Delete method of the IControl interface to remove a particular form control from worksheet. To remove all the controls from worksheet, you can use Clear method of the IControlCollection interface.
The code below demonstrates how to add or delete form controls to or from a worksheet:
var workbook = new Workbook();
IWorksheet ws = workbook.Worksheets["Sheet1"];
// Add two controls
var lblResolution = ws.Controls.AddLabel(12.6, 20.4, 49.2, 18.6);
lblResolution.Text = "Resolution";
lblResolution.PrintObject = true;
var btnNative = ws.Controls.AddButton(199.8, 21, 127.8, 17.4);
btnNative.Text = "Use native resolution";
btnNative.PrintObject = true;
// Remove the first control
ws.Controls[0].Delete();
// Remove all the controls
// ws.Controls.Clear()
The selection-based form controls, that are Checkbox, Option button, Listbox, Dropdown, and Scrollbar provide LinkedCell property of the ICellLinkControl interface that enables a two-way binding between value of the form control and the linked cell range. Linked cell range allows you to have a definite set of values in form control to avoid invalid data input from the end-user. You can update values of the form control by simply editing value in the linked cell range or vice-versa.
The code below shows how to link cell values to the Checkbox form control:
// Link a check box
var checkBox1 = ws.Controls.AddCheckBox(54, 13.2, 64.2, 18);
checkBox1.LinkedCell = ws.Range["$A$2"];
GcExcel uses zero-based indexing while placing the form controls on a worksheet. You can find a form control in the worksheet using its name or its type. To find an excel form control by its name, you can use nameOf operator to look for the specified name. While to find a control using its type, you can use the FormControlType enumeration.
See the code below to find the control by its name:
// add the control
var lblResolution = ws.Controls.AddLabel(12.6, 20.4, 49.2, 18.6);
lblResolution.Text = "Resolution";
lblResolution.PrintObject = true;
lblResolution.Name = nameof(lblResolution);
// find the control by name
Console.WriteLine(ws.Controls[nameof(lblResolution)].Name);
See the code below to find the control by its type:
// Get control by type and change width (without type conversion)
foreach (var ctl in ws.Controls)
{
switch (ctl.FormControlType)
{
case FormControlType.Button:
ctl.Width = 70;
break;
case FormControlType.CheckBox:
ctl.Width = 60;
break;
}
}
Worksheets with form controls can be exported to PDF, XLSX, XLSM, HTML, .sjs, or SSJSON formats using Save method of the Workbook class and to PNG, SVG, JPG, or GIF formats using ToImage method of the IWorksheet interface. GcExcel provides Visible property in IControl interface that enables you to include or exclude the form controls while exporting. If you set Visible property of a form control to false, then that form control is not exported to either PDF, XLSX, XLSM, .sjs, SSJSON, HTML, PNG, SVG, JPG, or GIF formats.
Refer to the following example code to exclude a form control from exporting:
// Add dropdown.
var dropDown = ws.Controls.AddDropDown(28.8, 81.8, 103.8, 31.4);
dropDown.PrintObject = true;
dropDown.Items.Add(new DropDownItem("Item 1"));
dropDown.Items.Add(new DropDownItem("Item 2"));
dropDown.Items.Add(new DropDownItem("Item 3"));
dropDown.SelectedIndex = 0;
// Set Visible to false.
dropDown.Visible = false;
type=warning
Note: GcExcel exports the form controls as static images in the PNG, SVG, JPG, GIF, HTML, or PDF format.
For exporting form controls to interactive form fields in PDF, see Export Form Controls to Form Fields.
GcExcel form controls are also shapes. Hence, to recognize whether a particular shape is a form control, ShapeType enumeration provides a FormControl member. To add onto this, if a shape is a form control, you can get the form control associated with the shape using the Control property of the IShape interface. Also, you can get shape associated with a form control using ShapeRange property of the IControl interface.
Refer to the following code to use form control as shape:
// Add form control
var button1 = ws.Controls.AddButton(50, 100, 120, 40);
var buttonShape1 = button1.ShapeRange[0];
// Duplicate
buttonShape1.Duplicate();
// Size and move
buttonShape1.Left = 66.6;
buttonShape1.Top = 22.8;
buttonShape1.Width = 155.4;
buttonShape1.Height = 49.2;
// Delete
buttonShape1.Delete();
In GcExcel, two or more option buttons can be grouped in a group box so that you can select one choice from several related but mutually exclusive choices. GcExcel groups the option buttons using the GroupBox property (read only) of IOptionButton interface which is identified by the boundaries of option buttons and group boxes. The GroupBox property is the first matched group box if an option button lies entirely within a group box. If there are no matching group boxes, the option button is in the default group, which is the worksheet.
When two or more option buttons are in the same group, they affect the selection state of other option buttons and allow you to select only one option button at a time in the same group. They also share the LinkedCell property, which means you can define LinkedCell for one option button in the group, and other option buttons in the same group can use the LinkedCell value.
type=warning
Note: Explicitly recalculate option button groups by calling Cut(Left,Top) on each group box control when a group box or option button loses focus.
Refer to the following example code to add two separate group boxes, each with respective linked option buttons:
// Initialize Workbook.
var workbook = new Workbook();
// Create a worksheet.
IWorksheet ws = workbook.Worksheets["Sheet1"];
// Add option buttons and group boxes to the worksheet.
var rngB2 = "Option buttons are grouped by group boxes.";
ws.Range["B2"].Value = rngB2;
ws.Range["B13:C13"].Value = new object[,] {
{ "Value", 1d}
};
ws.Range["E13:F13"].Value = new object[,] {
{ "Value", 2d}
};
ws.Range["A:A"].ColumnWidthInPixel = 37d;
// Add first group box.
var group1 = ws.Controls.AddGroupBox(29.75, 48.2, 136.5, 113.99);
group1.Text = "Group 1";
// Add option buttons.
var optionButton2 = ws.Controls.AddOptionButton(39.45, 67.1, 98, 15.60);
// Set linked cell.
optionButton2.LinkedCell = ws.Range["C13"];
optionButton2.IsChecked = true;
ws.Controls.AddOptionButton(39.45, 97.5, 98, 17.40);
ws.Controls.AddOptionButton(39.45, 131.2, 98, 17.5);
// Add second group box.
var group2 = ws.Controls.AddGroupBox(191.95, 48.2, 136.5, 113.99);
group2.Text = "Group 2";
// Add option buttons.
ws.Controls.AddOptionButton(200.35, 65.7, 117.6, 18.5);
var optionButton7 = ws.Controls.AddOptionButton(200.35, 95.99, 117.6, 21.28);
// Set linked cell.
optionButton7.LinkedCell = ws.Range["F13"];
optionButton7.IsChecked = true;
ws.Controls.AddOptionButton(200.35, 129.2, 117.6, 21.40);
// Save the workbook.
workbook.Save("OptionButtonsBasicUsage.xlsx");
type=warning
Note: Adding option buttons to overlapping group boxes may not be the same as in Microsoft Excel.
Limitations
Export of rich text and styles in GcExcel form controls is not supported.
Getting or setting macro names in form controls requires VBA project.
VBA project and COM interoperability is required for invoking macro when OnAction.
LinkedObject and ‘Copy to Clipboard’ features require Window OLE automation interoperability.
Spell check in Excel form controls requires Windows language pack interoperability.
There is loss of some nodes during Excel I/O of alternate content in drawing Xml and Vml.
In the option button group, some option buttons will join a different group when the measuring result differs from Excel.