Forms/Edit Fields

Top  Previous  Next

 

WPTools can be also used to create forms. These are sepecial texts which are generally protected. The user may only edit the text in specially marked areas. We call this areas 'Edit Fields':

 

clip0129

 

Edit fields work like mail merge fields. The only difference is that the objects use the mode flag "wpobjWithinEditable". When saved to RTF a \formfield instead of a \field tag is written. 

 

To create a field use procedure

InputEditField(const FieldName: string;

       DisplayText: string = '';

       PlaceCaret: Boolean = FALSE;

       Command: string = ''; // will be written to TWPTextObj.Source

       Format: Integer = 0 // will be written to TWPTextObj.IParam

): TWPTextObj;

 

All procedures which work with mail merge fields will work for the edit fields, too. Like with mail merge fields the presentation of the start and end markers is controlled by the property InsertPointAttr. The text within the markers is controlled by AutomaticTextAttr.

 

The editor for the example above had been set up with:

 

DataEdit.ProtectedProp := [ppAllExceptForEditFields];

DataEdit.EditOptionsEx := [wpTABMovesToNextEditField,wpRepaintOnFieldMove];

DataEdit.InsertPointAttr.Hidden := FALSE;

DataEdit.InsertPointAttr.CodeTextColor := $E0FFE0;

DataEdit.InsertPointAttr.CodeOpeningText := '[';

DataEdit.InsertPointAttr.CodeClosingText := ']';

 

The most important property change is ProtectedProp := [ppAllExceptForEditFields]. This makes it impossible for the cursor to move anywhere else than within edit field tags.

 

To make it possible to highlight the current field (yellow background) the property UseOnGetAttrColorEvent and the event OnGetAttributeColor has been used:

 

DataEdit.AutomaticTextAttr.UseOnGetAttrColorEvent := TRUE;

 

procedure TWPEdTest.DataEditGetAttributeColor(Sender: TObject;

var CharStyle: TCharacterAttr; par: TParagraph; posinpar: Integer);

var obj : TWPTextObj;

begin

obj := DataEdit.CodeInsideOf(par,posinpar,wpobjMergeField);

if obj = DataEdit.FieldAtCP then

begin

    CharStyle.BackgroundColor := $A0FFFF;

    CharStyle.UseBackgroundColor := TRUE;

end;

end;

 

Edit fields can be read out using procedure MailMerge and event OnMailMergeGetText:

 

// Read the data which is currently displayed in the editor

procedure TWPEdTest.ReadData;

begin

  FReadingData := TRUE; // global boolean to change behaviour

  DataEdit.MergeText;

end;

 

// Write back the data

procedure TWPEdTest.WriteData;

begin

  FReadingData := FALSE;

  DataEdit.MergeText;

end;

 

// This reads and writes the data fro the database 'Table1'

procedure TWPEdTest.DataEditMailMergeGetText(Sender: TObject;

const inspname: String; Contents: TWPMMInsertTextContents);

begin

    if FReadingData then

          Table1.FieldByName(inspname).AsString := Contents.OldText

    else Contents.StringValue := Table1.FieldByName(inspname).AsString;

end;

end;

 

This code can be used to move to a certain field. If the cusror is within a field with that name the next field will be located and selected.

 

procedure TWPEdTest.MoveToField(fieldname : String);

begin

  fieldname := SelectField2.Text;

  // If this is the current move on ...

  if DataEdit.CurrentEditField=fieldname then

       DataEdit.MoveToNextField(false);

  // Try from here

  if DataEdit.MoveToField(fieldname,false) then

     DataEdit.SelectFieldAtCP(false, true)

  // or from start

  else if DataEdit.MoveToField(fieldname,true) then

     DataEdit.SelectFieldAtCP(false, true);

  DataEdit.SetFocus;

end;