Implement a checkbox

<< Click to Display Table of Contents >>

Navigation:  Programming > Mail Merge (replace fields with data) and data forms > Forms & Edit Fields (data forms) >

Implement a checkbox

It is also possible to create check box entries inside of an edit field.

clip0002

We assign the name "_CHECK" to the field which holds the checkbox.

 

(The following code has been extracted from the demo project MailMerge\EditFields.)

 

// Insert checkbox

// Editfield wrapper with Cursor inside

  TemplateEdit.InputEditField('_CHECK','', true);

// And add a checkbox

  TemplateEdit.InputTextFieldName('FORMCHECKBOX',

       SelectField.Text,

       FieldNames.Lines.Values[SelectField.Text]

     );

// leave the field

  TemplateEdit.CPMoveNext;

 

Checkboxes are actually textobjects, not merge fields which store their value in the "Params" property.

The object FORMCHECKBOX accept as positive value in Params: true, yes, T, 1 and as negative '', false, no, F, 0.

Since they are text objects they can be controlled through special code in the event OnTextObjectMouseDown  to change their value on mousedown:

 

procedure TWPEdTest.DataEditTextObjectMouseDown(Sender: TWPCustomRtfEdit;

 pobj: TWPTextObj; obj: TWPObject; Button: TMouseButton;

 Shift: TShiftState; X, Y: Integer);

begin

if pobj.Name = 'FORMCHECKBOX' then

begin

    WPSetCheckBoxValue(pobj,not WPGetCheckBoxValue(pobj,true)); // utility function to swap true/false, 1/0, T/F and on/off

    DataEdit.IgnoreMouse;

    DataEdit.Repaint;

end;

end;

 

The code above is automatically executed (no coding required) if the flag wpInteractiveFORMTextObjects was set in ViewOptionsEx.

 

Use checkbox in mail merge:

 

Since the checkbox is wrapped by a mail merge or edit field, it can also be updated by the merge text procedure.

To make this easy, the object TWPMMInsertTextContents has methods SetBoolean and GetBoolean to update the state of the checkbox.