Visual Basic User's Guide > Using Marked Text Fields
The source code for this example is contained in the Samples\vb6\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:
[Visual Basic 6] Private Sub btnOK_Click() Form1.TXTextControl1.FieldInsert "" Form1.TXTextControl1.FieldData( _ Form1.TXTextControl1.FieldCurrent) = Text1 Form1.TXTextControl1.SelLength = 0 Unload Me End Sub
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:
[Visual Basic 6] Private Sub Form_Load() Dim nFieldID As Integer nFieldID = 0 ' Fill the combobox with bookmarks Do nFieldID = Form1.TXTextControl1.FieldNext(nFieldID, 0) If nFieldID > 0 Then cboBookmark.AddItem _ Form1.TXTextControl1.FieldData(nFieldID) End If Loop While nFieldID <> 0 ' Copy the first item to the edit control part of ' the combo box cboBookmark.Text = cboBookmark.List(0) End Sub
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.
[Visual Basic 6] Private Sub cmdOk_Click() Dim nFieldID As Integer nFieldID = 0 ' Search for the requested bookmark Do nFieldID = Form1.TXTextControl1.FieldNext(nFieldID, 0) If nFieldID > 0 Then If Form1.TXTextControl1.FieldData(nFieldID) = _ cboBookmark.Text Then Exit Do End If End If Loop While nFieldID <> 0 ' If the bookmark has been found, select it. ' Text Control will then automatically scroll to ' make it visible. If nFieldID <> 0 Then Form1.TXTextControl1.FieldCurrent = nFieldID Form1.TXTextControl1.SelStart = _ Form1.TXTextControl1.FieldStart - 1 Form1.TXTextControl1.SelLength = _ Form1.TXTextControl1.FieldEnd - _ Form1.TXTextControl1.FieldStart + 1 Else MsgBox "Bookmark not found." End If Unload Me End Sub
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.