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 mnuBookmark_Insert_Click(object sender, System.EventArgs e)
{
    if (textControl1.Text == "")
    {
        MessageBox.Show("Cannot insert a bookmark if the Text Control is empty.");
    }
    else if (textControl1.Selection.Length == 0)
    {
        textControl1.Selection.Length = 1;
    }
    else 
    {
        TXTextControl.TextField newField = new TXTextControl.TextField();
        newField.ID = fieldID;
        newField.Text = textControl1.Selection.Text;
        textControl1.Selection.Text = "";
        fieldID += 1;
        textControl1.TextFields.Add(newField);
    }
}
[Visual Basic]
Private Sub mnuBookmark_Insert_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuBookmark_Insert.Click
    If TextControl1.Text = "" Then
        MsgBox("Cannot insert a bookmark if the Text Control is empty.")
    ElseIf TextControl1.Selection.Length = 0 Then
        TextControl1.Selection.Length = 1
    Else
        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
[Delphi]
procedure Form1.mnuBookmark_Insert_Click(sender: System.Object; 
    e: System.EventArgs);
var
    NewField :TXTextControl.TextField;
begin
    if TextControl1.Text = '' then
        MessageBox.Show('Cannot insert a bookmark if the '
            +'Text Control is empty.')
    else
      If TextControl1.Selection.Length = 0 then
        TextControl1.Selection.Length := 1
    else begin
        NewField := TXTextControl.TextField.Create;
        NewField.ID := FieldID;
        NewField.Text := TextControl1.Selection.Text;
        TextControl1.Selection.Text := '';
        FieldID := FieldID + 1;
        TextControl1.TextFields.Add(NewField);
    end;
end;

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 cmdOK_Click(object sender, System.EventArgs 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;
            }
        }
    }
    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

    If (CInt(TextBox1.Text) > tx.TextFields.Count) Then
        MsgBox("Invalid bookmark number!")
    Else
        For Each Field In tx.TextFields
            If (Field.ID = CInt(TextBox1.Text)) Then
                tx.Selection.Start = Field.Start - 1
                tx.Selection.Length = Field.Length
            End If
        Next
    End If
    Close()
End Sub
[Delphi]
procedure frmGoto.cmdOK_Click(sender: System.Object; e: System.EventArgs);
var
    Field : TXTextControl.TextField;

begin
    if (StrToInt(TextBox1.Text) > tx.TextFields.Count) then begin
        MessageBox.Show('Invalid bookmark number!');
    end
    else begin
        for Field in tx.TextFields do begin
            if (Field.ID = StrToInt(TextBox1.Text)) then begin
               tx.Selection.Start := Field.Start - 1;
               tx.Selection.Length := Field.Length;
            end;
        end;
    end;
    Close;
end;

<< Previous Next >>