Back to Devexpress

Form Controls in Spreadsheet Documents

officefileapi-404708-spreadsheet-document-api-form-controls.md

latest9.2 KB
Original Source

Form Controls in Spreadsheet Documents

  • Nov 28, 2023
  • 4 minutes to read

Form controls controls are interactive elements (buttons, checkboxes, and drop-down lists) that can be added to a worksheet to create user-friendly interfaces. These controls allow users to input or manage data.

The Spreadsheet Document API allows you to manage form controls in code. You can add, edit and remove form controls. Documents with form controls are processed without content loss.

Form controls are available in the following formats:

  • XLSX
  • XLSM
  • XLTX
  • XLTM

Create Form Controls

The table below lists available form controls and API used to create each type.

Form ControlClassMethod
ButtonButtonFormControlFormControlCollection.AddButton
Check BoxCheckBoxFormControlFormControlCollection.AddCheckBox
Combo boxComboBoxFormControlFormControlCollection.AddComboBox
Group boxGroupBoxFormControlFormControlCollection.AddGroupBox
List boxListBoxFormControlFormControlCollection.AddListBox
Radio ButtonRadioButtonFormControlFormControlCollection.AddRadioButton
ScrollbarScrollbarFormControlFormControlCollection.AddScrollbar
Spin ButtonSpinnerFormControlFormControlCollection.AddSpinner

Note

The FormControlCollection.Add... method call adds a new item to both FormControlCollection and ShapeCollection.

The code sample below creates a button, a list box, and a checkbox. The result is opened in WinForms Spreadsheet Control:

csharp
using DevExpress.Spreadsheet;

Workbook workbook = new Workbook();

var formControls = workbook.Worksheets[0].FormControls;

// Create a button form control:
var buttonCellRange = workbook.Worksheets[0].Range["B2:C2"];
var buttonFormControl = formControls.AddButton(buttonCellRange);
buttonFormControl.PlainText = "Click Here";

// Create a list box form control:
var comboCellRange = workbook.Worksheets[0].Range["B4:C4"];
var comboBoxControl = formControls.AddComboBox(comboCellRange);
comboBoxControl.DropDownLines = 3;
comboBoxControl.SourceRange = workbook.Worksheets[0].Range["E2:E6"];
comboBoxControl.SelectedIndex = 1;

// Create a check box form control:
var checkRange = workbook.Worksheets[0].Range["D5:E5"];
var checkBoxControl = formControls.AddCheckBox(checkRange);
checkBoxControl.CheckState = FormControlCheckState.Checked;
checkBoxControl.PlainText = "Reviewed";
vb
Imports DevExpress.Spreadsheet

Private workbook As New Workbook()

Private formControls = workbook.Worksheets(0).FormControls

' Create a button form control:
Private buttonCellRange = workbook.Worksheets(0).Range("B2:C2")
Private buttonFormControl = formControls.AddButton(buttonCellRange)
buttonFormControl.PlainText = "Click Here"

' Create a list box form control:
Dim comboCellRange = workbook.Worksheets(0).Range("B4:C4")
Dim comboBoxControl = formControls.AddComboBox(comboCellRange)
comboBoxControl.DropDownLines = 3
comboBoxControl.SourceRange = workbook.Worksheets(0).Range("E2:E6")
comboBoxControl.SelectedIndex = 1

' Create a check box form control:
Dim checkRange = workbook.Worksheets(0).Range("D5:E5")
Dim checkBoxControl = formControls.AddCheckBox(checkRange)
checkBoxControl.CheckState = FormControlCheckState.Checked
checkBoxControl.PlainText = "Reviewed"

Access and Modify Form Controls

The Worksheet.FormControls property obtains all form controls in a worksheet. You can use one of the following ways to obtain a specific form control:

The FormControl.Id and FormControl.Name properties return the form control’s identifier and name.

The code sample below retrieves all button form controls from a worksheet and disables printing for all buttons:

csharp
using DevExpress.Spreadsheet;
using System.Linq;

Workbook workbook = new Workbook();
workbook.LoadDocument("Document.xlsx");

var formControls = workbook.Worksheets[0].FormControls;

var buttons = formControls.Where(formControl => formControl.FormControlType == FormControlType.Button).Cast<ButtonFormControl>();
foreach (ButtonFormControl button in buttons) {
    button.PrintObject = false;
}
vb
Imports DevExpress.Spreadsheet
Imports System.Linq

Private workbook As New Workbook()
workbook.LoadDocument("Document.xlsx")

Dim formControls = workbook.Worksheets(0).FormControls

Dim buttons = formControls.Where(Function(formControl) formControl.FormControlType = FormControlType.Button).Cast(Of ButtonFormControl)()
For Each button As ButtonFormControl In buttons
  button.PrintObject = False
Next button

You can print and export to PDF workbooks with form controls. Each form control has the PrintObject property that specifies whether to print the form control.

This option also affects the following operations:

  • Form control export to PDF format
  • Export of a cell range that contains a form control to an image
  • Export of a worksheet with form controls to an image

Remove Form Controls

Call the FormControlCollection.Remove or FormControlCollection.RemoveAt method to remove a form control. The FormControlCollection.Clear() method removes all form controls from a worksheet.

The code sample below removes all check boxes from a workbook:

csharp
Workbook workbook = new Workbook();
workbook.LoadDocument("Document.xlsx");

var formControls = workbook.Worksheets[0].FormControls;

for (int i = formControls.Count - 1; i >= 0; i--) {
    if (formControls[i].FormControlType == FormControlType.CheckBox)
        formControls.RemoveAt(i);
}
vb
Dim workbook As New Workbook()
workbook.LoadDocument("Document.xlsx")

Dim formControls = workbook.Worksheets(0).FormControls

For i As Integer = formControls.Count - 1 To 0 Step -1
  If formControls(i).FormControlType = FormControlType.CheckBox Then
    formControls.RemoveAt(i)
  End If
Next i

Limitations

The Form Control API subset ships with the following limitations: