Delphi User's Guide > Using Marked Text Fields
This first sample program will show you how fields are created and what happens when they are clicked on. The code shown here is contained in the Samples\Delphi\Field1 sample source directory.
The program consists of a form with just one menu item, Insert Field!, with an exclamation mark to say that clicking on this item will cause an immediate action instead of dropping a menu. There are two Text Controls on the form, one of which is used as a normal text window (TXTextControl1), the other one as a pop-up window (TXTextControl2).
The following code is executed when the menu item is clicked on:
procedure TForm1.Insertfield1Click(Sender: TObject); begin TXTextControl1.FieldInsert ('-------'); TXTextControl1.FieldEditAttr[ TXTextControl1.FieldCurrent] := $10 + $2 + $1; end;
This inserts a field at the current caret position and sets its attributes, so it is displayed with a gray background and a second input position in front of and right behind a field.If you move the cursor over the field, Text Control changes the mouse pointer to an upward pointing arrow to indicate that there is something to click on.
If you click on the field, the application receives a FieldClicked event, to which it responds by popping up a window which displays the field number.
Only five lines of code are required for this:
procedure TForm1.TXTextControl1FieldClicked(Sender: TObject; FieldIndex: Smallint); begin TXTextControl1.FieldCurrent := FieldIndex; TXTextControl2.Text := 'This is field no. ' + InttoStr(FieldIndex) + '. Its text is: ' + TXTextControl1.FieldText; TXTextControl2.Left := toPixels(TXTextControl1.FieldPosX); TXTextControl2.Top := toPixels(TXTextControl1.FieldPosY); TXTextControl2.BringtoFront; end;
The first line selects the marked text field which has been clicked on. Line 2 builds the string that is to be displayed in the pop-up window. Line 3 and 4 moves the pop-up window, which is initially hidden behind the text window, to the position of the marked text field. Line 5 puts the pop-up window in front of the text window to make it visible. When the mouse button is released, the text window is moved to the front again:
procedure TForm1.TXTextControl1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin TXTextControl1.BringtoFront; end;