Step 2: Adding a Dialog Box for Inserting Hypertext Links

In this second sample program, a dialog box is created which enables you to insert hypertext links in a more convenient way. Additionally, hypertext links which have previously been inserted or loaded from a file, can be edited and modified. Note that, while hypertext links are usually associated with HTML files, they can as well be stored in RTF or Microsoft Word files, or in TX Text Control's proprietary format.

The dialog box contains two text boxes. The first is for the text that represents the hypertext link in the document and the second is for the address, to which the link points.

The same dialog box is used for both, inserting a new and editing an existing hypertext link. Depending on whether the current input position is inside an existing link, this link is modified. Otherwise, a new one is inserted.

[C#]
private void mnuInsert_Hyperlink_Click(object sender, System.EventArgs e)
{
    frmHyperlinks Hyperlinks = new frmHyperlinks();
    Hyperlinks.tx = textControl1;
    Hyperlinks.ShowDialog(); 
    HighlightHyperlinks (textControl1, mnuView_Hyperlinks.Checked);
}
[Visual Basic]
Private Sub mnuInsert_Hyperlink_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles mnuInsert_Hyperlink.Click
    Dim Hyperlinks As New frmHyperlinks
    Hyperlinks.tx = TextControl1
    Hyperlinks.ShowDialog()
    HighlightHyperlinks(TextControl1, mnuView_Hyperlinks.Checked)
End Sub

When the form is loaded, the text boxes are filled with the text and link information, provided that the current input position is inside a link:

[C#]
private void frmHyperlinks_Load(object sender, System.EventArgs e)
{
    TXTextControl.HypertextLink HyperLink = tx.HypertextLinks.GetItem();
    if (HyperLink != null)
    {
        // If there is an existing hypertext link at the input position,
        // copy its text and target to the text boxes on the form.
        txtLinkedText.Text = HyperLink.Text;
        txtLinkTo.Text = HyperLink.Target;
    }
    else 
    {
        // If there is no hypertext link at the input position, but
        // some text has been selected, then copy this text to the
        // Linked Text text box.
        if  (tx.Selection.Length > 0)  
            txtLinkedText.Text = tx.Selection.Text; 
    }
}
[Visual Basic]
Private Sub frmHyperlinks_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim HyperLink As TXTextControl.HypertextLink = tx.HypertextLinks.GetItem()
    If Not (HyperLink Is Nothing) Then
        ' If there is an existing hypertext link at the input position,
        ' copy its text and target to the text boxes on the form.
        txtLinkedText.Text = HyperLink.Text
        txtLinkTo.Text = HyperLink.Target
    Else
        ' If there is no hypertext link at the input position, but
        ' some text has been selected, then copy this text to the
        ' Linked Text text box.
        If (tx.Selection.Length > 0) Then
            txtLinkedText.Text = tx.Selection.Text
        End If
    End If
End Sub

The user then can change the displayed information. On clicking the OK button, the information is transfered to the document by either inserting a new link or modifying the existing one:

[C#]
private void cmdOK_Click(object sender, System.EventArgs e)
{
    TXTextControl.HypertextLink HyperLink = tx.HypertextLinks.GetItem();
    if (HyperLink == null)
    {
        // Insert a new link
        TXTextControl.HypertextLink NewLink = 
            new TXTextControl.HypertextLink(
            txtLinkedText.Text , txtLinkTo.Text);
        tx.HypertextLinks.Add(NewLink);
    } 
    else 
    {
        // Update an existing link
        HyperLink.Text = txtLinkedText.Text;
        HyperLink.Target = txtLinkTo.Text;
    }
    Close();
}
[Visual Basic]
Private Sub cmdOK_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles cmdOK.Click
    Dim HyperLink As TXTextControl.HypertextLink = tx.HypertextLinks.GetItem()
    If HyperLink Is Nothing Then
        ' Insert a new link
        Dim NewLink As New TXTextControl.HypertextLink( _
            txtLinkedText.Text, txtLinkTo.Text)
        tx.HypertextLinks.Add(NewLink)
    Else
        ' Update an existing link
        HyperLink.Text = txtLinkedText.Text
        HyperLink.Target = txtLinkTo.Text
    End If
    Close()
End Sub

Finally, there is a menu item to switch the character format of the hyperlink's text to blue colored and underlined style. The menu item calls the function HighlightHyperlinks, which is not a Text Control method, but part of the sample program. It changes each element of the Text Control's HypertextLinkCollection to the desired display style.

[C#]
void HighlightHyperlinks(TXTextControl.TextControl tx, bool bHighlight)
{
    int PreviousStart = tx.Selection.Start,
        PreviousLength = tx.Selection.Length;

    foreach (TXTextControl.HypertextLink Link in tx.HypertextLinks) 
    {
        tx.Selection.Start = Link.Start-1;
        tx.Selection.Length = Link.Length;
        if (bHighlight) 
        {
            tx.Selection.ForeColor = Color.Blue;
            tx.Selection.Underline = TXTextControl.FontUnderlineStyle.Single;
        } 
        else 
        {
            tx.Selection.ForeColor = Color.Black;
            tx.Selection.Underline = TXTextControl.FontUnderlineStyle.None;
        }
    }
    tx.Selection.Start = PreviousStart;
    tx.Selection.Length = PreviousLength;
}
[Visual Basic]
Private Sub HighlightHyperlinks(ByVal tx As TXTextControl.TextControl, _
    ByVal Highlight As Boolean)
    Dim PreviousStart As Integer = tx.Selection.Start 
    Dim PreviousLength As Integer = tx.Selection.Length
    For Each Link As TXTextControl.HypertextLink In tx.HypertextLinks
        tx.Selection.Start = Link.Start - 1
        tx.Selection.Length = Link.Length
        If Highlight Then
            tx.Selection.ForeColor = Color.Blue
            tx.Selection.Underline = TXTextControl.FontUnderlineStyle.Single
        Else
            tx.Selection.ForeColor = Color.Black
            tx.Selection.Underline = TXTextControl.FontUnderlineStyle.None
        End If
        tx.Selection.Start = PreviousStart
        tx.Selection.Length = PreviousLength
    Next
End Sub

<< Previous Next >>