The source code for this example is contained in the Samples\Delphi\Field3 sample source directory.
In commercial word processors, bookmarks are normally referenced by names, not just by numbers. The names are typed in by the user when he creates a bookmark. The Goto Bookmark dialog box then presents a listbox or combobox in which one of the strings can be selected.
The Insert Bookmark... menu item in this version of the program creates a dialog box where the user can enter a label for the bookmark.
When the OK button is clicked, the following code is executed:
procedure TfrmInsert.btnOKClick(Sender: TObject);var fieldID : Integer; begin If Form1.TXTextControl1.SelLength = 0 Then begin If Form1.TXTextControl1.SelStart = Length(Form1.TXTextControl1.Text) Then begin Form1.TXTextControl1.SelText := ' '; Form1.TXTextControl1.SelStart := Form1.TXTextControl1.SelStart - 1; end; Form1.TXTextControl1.SelLength := 1; end; // Insert a field and store the bookmark name in its // FieldData property Form1.TXTextControl1.FieldInsert (''); fieldID := Form1.TXTextControl1.FieldCurrent; Form1.TXTextControl1.FieldEditAttr [fieldID] := $10 + $2 + $1; Form1.TXTextControl1.FieldData [Form1.TXTextControl1.FieldCurrent] := Edit1.Text; Form1.TXTextControl1.SelLength := 0; Close; end;
First, a marked text field is created at the current caret position. Second, the name of the bookmark, which is the text that has been typed in by the user, is stored in the FieldData property.
The Goto Bookmark dialog box contains a combo box which lists all of the bookmarks which have been created so far.
The combo box is filled with the bookmark titles when its form is loaded:
procedure TfrmGoto.FormShow(Sender: TObject); var nfieldID : Integer; begin nFieldID := 0; cboBookmark.Clear; // Fill the combobox with bookmarks Repeat nFieldID := Form1.TXTextControl1.FieldNext( nfieldID, 0); If nFieldID > 0 Then cboBookmark.Items.Add( Form1.TXTextControl1.FieldData[nFieldID]) Until nFieldID = 0; // Copy first item to the edit control part of the // combo box cboBookmark.Text := cboBookmark.Items.Strings; end;
When the OK button is clicked, the bookmark list is searched for the string which has been selected in the combo box, and the corresponding marked text field is selected.
procedure TfrmGoto.Button1Click(Sender: TObject); var nFieldID : Integer; label Exit; begin nFieldID := 0; // Search for the requested bookmark Repeat nFieldID := Form1.TXTextControl1.FieldNext( nFieldID, 0); If nFieldID > 0 Then begin If Form1.TXTextControl1.FieldData[nFieldID] = (cboBookmark.Text) Then begin Goto Exit; end; end; until nFieldID = 0; Exit: // If the bookmark has been found, select it. // Text Control will then automatically scroll // to make it visible If nFieldID <> 0 Then begin Form1.TXTextControl1.FieldCurrent := nFieldID; Form1.TXTextControl1.SelStart := Form1.TXTextControl1.FieldStart - 1; Form1.TXTextControl1.SelLength := Form1.TXTextControl1.FieldEnd - Form1.TXTextControl1.FieldStart + 1; end Else Application.MessageBox( 'Bookmark not found.','ERROR',MB_OK); Close; end;
You can also extend the sample program with a dialog box, similar to the Go To Bookmark... dialog, in which a bookmark can be deleted without deleting the text. This would require converting the Marked Text Field to normal text. Use the FieldDelete method to achieve this.
More information about marked text fields and a list of all properties, methods and events that can be used with marked text fields, can be found in the chapter Technical Articles - Marked Text Fields.