Bookmarks

This example shows you how to use 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 following directories:

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

[C#]
private void MenuItem_Click_1(object sender, RoutedEventArgs e)
{
    if (textControl1.Text == "")
    {
        MessageBox.Show("Cannot insert a bookmark if the Text Control is empty.");
        return;
    }
    else if (textControl1.Selection.Length == 0)
    {
        textControl1.Selection.Length = 1;
    }


    // Now turn the selected text into a new field
    TXTextControl.TextField NewField = new TXTextControl.TextField();
    NewField.ID = fieldID;
    NewField.Text = textControl1.Selection.Text;
    textControl1.Selection.Text = "";
    fieldID += 1;
    textControl1.TextFields.Add(NewField);
    textControl1.Focus();
}
[Visual Basic]
Private Sub MenuItem_Click_1(ByVal sender As Object, ByVal e As RoutedEventArgs)
    If textControl1.Text = "" Then
        MessageBox.Show("Cannot insert a bookmark if the Text Control is empty.")
    ElseIf textControl1.Selection.Length = 0 Then
        textControl1.Selection.Length = 1
    Else
        ' Now turn the selected text into a new field
        Dim NewField As New TXTextControl.TextField()
        NewField.ID = fieldID
        NewField.Text = textControl1.Selection.Text
        textControl1.Selection.Text = ""
        fieldID += 1
        textControl1.TextFields.Add(NewField)
    End If
End Sub

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.

Clicking the OK button executes the following procedure:

[C#]
private void button2_Click(object sender, RoutedEventArgs e)
{
    if (Convert.ToInt32(textBox1.Text) > tx.TextFields.Count)
    {
        MessageBox.Show("Invalid bookmark number!");
    }
    else
    {
        foreach (TXTextControl.TextField Field in tx.TextFields)
        {
            if (Field.ID == Convert.ToInt32(textBox1.Text))
            {
                tx.Selection.Start = Field.Start - 1;
                tx.Selection.Length = Field.Length;
            }
        }
    }
}
[Visual Basic]
Private Sub button2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    If Convert.ToInt32(textBox1.Text) > tx.TextFields.Count Then
        MessageBox.Show("Invalid bookmark number!")
    Else
        For Each Field As TXTextControl.TextField In tx.TextFields
            If Field.ID = Convert.ToInt32(textBox1.Text) Then
                tx.Selection.Start = Field.Start - 1
                tx.Selection.Length = Field.Length
            End If
        Next
    End If
End Sub

<< Previous Next >>