TX Text Control Source Code Library

Inserting and processing MS Word compatible FORMCHECKBOX fields

Checkboxes are probably the most commonly used selection elements in paper and web-based forms and questionnaires. The following article shows how to insert and process MS Word compatible checkboxes.

Checkbox sample

The checkbox form fields are handled by TX Text Control's ApplicationField class that provides an interface to retrieve and change the data and parameters of such fields.

Additionally, the FieldAdapter classes are utilized in this sample. The FieldAdapter classes are part of the DocumentServer namespace. They implement adapters for the most commonly used fields like DATE, MERGEFIELD or FORMCHECKBOX.

The following code creates and inserts a checkbox field:

// an Unicode font is used to simulate the check box textControl1.Selection.FontName = "Arial Unicode MS"; // create a new checkbox using the specific adapter field class FormCheckBox clsCheckBox = new TXTextControl.DocumentServer.Fields.FormCheckBox(); clsCheckBox.Text = UNCHECKED; clsCheckBox.Enabled = true; clsCheckBox.ApplicationField.Editable = false; clsCheckBox.ApplicationField.DoubledInputPosition = true; // add the ApplicationField to the document textControl1.ApplicationFields.Add(clsCheckBox.ApplicationField);

The UNCHECKED string is a Unicode string ("\\u2610") that represents an unchecked box.

If the user clicks on a field, the specific field will be casted to a FormCheckBox field in order to switch the text from checked to unchecked or vice versa.

private void textControl1_TextFieldClicked(object sender, TXTextControl.TextFieldEventArgs e) { // cast the field to an ApplicationField ApplicationField field = e.TextField as ApplicationField; if (field != null) { // check whether the field is a checkbox field if ((field.TypeName == "FORMCHECKBOX")) { // create a new adapter field FormCheckBox chkb = new FormCheckBox(field); if (chkb.Enabled == false) return; // change the checked state if (field.Text == UNCHECKED) { chkb.Checked = true; chkb.Text = CHECKED; } else if (field.Text == CHECKED) { chkb.Checked = false; chkb.Text = UNCHECKED; } } } }

These fields are handled like normal text fields and can be moved within the text like any other characters. These chechboxes are compatible to MS Word and are be maintained on loading or saving documents as DOCX, DOC or RTF.

The minimum requirements for this sample application are TX Text Control .NET for Windows Forms 15.1 trial version and Visual Studio 2008.

 
 

Products

Support

Downloads

Corporate

Buy Now