Adding Strings to 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 btnOK_Click(object sender, RoutedEventArgs e)
{
    TXTextControl.TextField Field = new TXTextControl.TextField();

    Field.Name = tbFieldName.Text;
    Field.Text = tx.Selection.Text;
    tx.Selection.Text = "";

    if (!tx.TextFields.Add(Field))
        MessageBox.Show("Could not insert a bookmark. The cursor is probably inside a text field.");

    this.Close();
}
[Visual Basic]
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim Field As New TXTextControl.TextField()

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

    If Not tx.TextFields.Add(Field) Then
        MessageBox.Show("Could not insert a bookmark. The cursor is probably inside a text field.")
    End If

    Me.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 Window is loaded:

[C#]
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    foreach (TXTextControl.TextField Field in tx.TextFields)
    {
        lbFields.Items.Add(Field.Name);
    }
}
[Visual Basic]
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    For Each Field As TXTextControl.TextField In tx.TextFields
        lbFields.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 btnOK_Click(object sender, RoutedEventArgs e)
{
    foreach (TXTextControl.TextField Field in tx.TextFields)
    {
        if (Field.Name == lbFields.SelectedItem.ToString())
        {
            tx.Selection.Start = Field.Start - 1;
            tx.Selection.Length = Field.Length;
        }
    }

    this.Close();
}
[Visual Basic]
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    For Each Field As TXTextControl.TextField In tx.TextFields
        If Field.Name = lbFields.SelectedItem.ToString() Then
            tx.Selection.Start = Field.Start - 1
            tx.Selection.Length = Field.Length
        End If
    Next

    Me.Close()
End Sub

<< Previous Next >>