Bookmarks

This example shows you how to use Text Control's marked text fields to create bookmarks. The first version will reference the bookmarks simply by their field numbers. The source code for this example is contained in the Samples\Delphi\Field2 sample source directory.

The sample application has a Bookmark menu with two items which are named Insert and Go to.... Clicking Insert creates a marked text field at the current caret position. If a text selection exists, the selected text is converted into a field. If not, the character next to the caret is selected.

procedure TForm1.Insert1Click(Sender: TObject);
var
   fieldID : Integer;
begin
   If TXTextControl1.Text = '' Then
      Application.MessageBox ('Cannot insert a bookmark
         if the Text Control is empty.','ERROR',MB_OK)
   Else begin
      If TXTextControl1.SelLength = 0 Then
         TXTextControl1.SelLength := 1;
      TXTextControl1.FieldInsert ('');
      fieldID := TXTextControl1.FieldCurrent;
      TXTextControl1.FieldEditAttr[fieldID] :=
            $10 + $2 + $1;
   end;
end;

After typing in some text and inserting a few bookmarks, select the Go To... menu item. This will launch a dialog box which allows you to enter the number of the bookmark to jump to. There is no error processing in this example, so if you enter the number of a non-existent field, nothing will happen.

Clicking the OK button executes the following procedure:

procedure TfrmGoto.Button1Click(Sender: TObject);
begin
   Form1.TXTextControl1.FieldCurrent :=
      StrtoInt(Edit1.Text);
   Form1.TXTextControl1.SelStart :=
      Form1.TXTextControl1.FieldStart - 1;
   Form1.TXTextControl1.SelLength :=
      Form1.TXTextControl1.FieldEnd
         - Form1.TXTextControl1.FieldStart + 1;
   Close;
end;

The number which has been entered in the dialog box is taken as a value for the FieldCurrent property.