Change width of field

<< Click to Display Table of Contents >>

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

Change width of field

If you need your fields to mimic usual form data fields you can use the event OnEditFieldGetSize. This event will be triggered for the closing objects of the edit field. Using this event it is possible to change the width of the closing object depending on the width of the text which has already been entered. So the field can appear to have the same width in the form, no matter if 5 or 15 characters have been entered.

 

In this example we reserve space for approx 30 characters for a field:

 

procedure TWPEdTest.DataEditEditFieldGetSize(Sender: TObject;

const InspName: String; var EndmarkWidthTW: Integer;

var Option: TWPEditFieldAlignOpt; CurrentTextWidthTW,

 CurrentTextCharCount: Integer; par: TParagraph; posinpar: Integer;

 FieldObject: TWPTextObj);

begin

  // Edit fields should be fixed to 30 characters

  if not (wpFieldHasFORMCHECKBOX in Option) then

  with (Sender as TWPRTFEnginePaint) do

  begin

    if _CurrEditFieldGetSizeTextXOFF>CurrentTextWidthTW then

         EndmarkWidthTW := EndmarkWidthTW + _CurrEditFieldGetSizeCharWidth * 30 - CurrentTextWidthTW

    else EndmarkWidthTW := EndmarkWidthTW + _CurrEditFieldGetSizeCharWidth * 30 - _CurrEditFieldGetSizeTextXOFF;//CurrentTextWidthTW;

  end;

end;

 

The code uses this variables of the TWPRTFEnginePaint object:

 

_CurrEditFieldGetSizeTextXOFF: this is the offset of the closing field object from left border of the text

_CurrEditFieldGetSizeCharWidth: this is the avarage width for a character, it is calculated by TextWidth('Aa') div 2.

 

This parameters are passed to the event:

 

Sender: TObject - this is the edit engine, type TWPRTFEnginePaint
const InspName : String - this is the name of the field

var EndmarkWidthTW: Integer - modify this to create a visual field

var Option: TWPEditFieldAlignOpt - options. wpFieldHasFORMCHECKBOX is defined if the field contains a checkbox

CurrentTextWidthTW: The width of the text inside the field

CurrentTextCharCount: Integer: The count of characters

par: TParagraph: The current paragraph
posinpar: Integer - the position of the closing object
FieldObject: TWPTextObj - the field object

 

 

 

For an alternative way to solve the problem see TWPTextObjectClasses - described in the introduction.

The Items in this collection provide the event CalcSize which basically does the same as shown above.