Adding Strings to Marked Text Fields

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 source code for this example is contained in the following directories:

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:

[C#]
private void cmdOK_Click(object sender, System.EventArgs e)
{
    TXTextControl.TextField field = new TXTextControl.TextField();

    field.Name = textBox1.Text;
    field.Text = tx.Selection.Text;
    tx.Selection.Text = "";


    tx.TextFields.Add(Field);
    Close();
}
[Visual Basic]
Private Sub cmdOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdOK.Click
    Dim Field As New TXTextControl.TextField()

    Field.Name = TextBox1.Text
    Field.Text = tx.Selection.Text
    tx.Selection.Text = ""

    tx.TextFields.Add(field)
    Close()
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 TextField.Name 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:

[C#]
private void frmGotoDialog_Load(object sender, System.EventArgs e)
{
    foreach (TXTextControl.TextField Field in tx.TextFields) {
        ComboBox1.Items.Add(Field.Name);
    }
}
[Visual Basic]
Private Sub frmGotoDialog_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    Dim field As TXTextControl.TextField

    For Each field In tx.TextFields
        ComboBox1.Items.Add(field.Name)
    Next
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.

[C#]
private void cmdOK_Click(object sender, System.EventArgs e)
{
    foreach (TXTextControl.TextField field in tx.TextFields) 
    {
        if (field.Name == comboBox1.Text)
        {
            tx.Selection.Start = field.Start - 1;
            tx.Selection.Length = field.Length;
        }
    }
    Close();
}
[Visual Basic]
Private Sub cmdOK_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdOK.Click
    Dim Field As TXTextControl.TextField

    For Each Field In tx.TextFields
        If (Field.Name = ComboBox1.Items.Item(ComboBox1.SelectedIndex)) Then
            tx.Selection.Start = Field.Start - 1
            tx.Selection.Length = Field.Length
        End If
    Next
    Close()
End Sub

<< Previous Next >>