Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
Syndicate this content

Hypertext Links

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic .NET
Version:1.0
Released:August 05, 2004
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic 6.0
Download code:hyperlinks.zip Download [82.26 KB, ZIP]

Using Hypertext Links in TX Text Control .NET

In this sample, we are going to show you how to add hypertext links and targets to your documents, and how to respond to events fired by TX Text Control when a hypertext link is clicked. For an overview of the various classes, properties, methods and events involved, please refer to the Hypertext Links overview in the TX Text Control reference manual.

The source code for Visual Studio 2003 for the following examples is contained in the subfolders Step1 to Step3 of the HyperLinks sample source directory.

Step 1: Inserting a Hypertext Link

In this first sample program, a hypertext link will be inserted in to a text document. The document is then saved as an HTML file so that it can be displayed in a web browser. This is what the sample program looks like:

To insert a hypertext link, we first need to create a HypertextLinks collection.

  1. ' Visual Basic
  2. Private Sub cmdInsertHyperlink_Click(ByVal sender As System.Object, _
  3. ByVal e As System.EventArgs) Handles cmdInsertHyperlink.Click
  4.  
  5. ' Create a HypertextLink object
  6. Dim MyLink As New TXTextControl.HypertextLink( _
  7. "TX Text Control Web Site", _
  8. "http://www.textcontrol.com")
  9.  
  10. ' Insert the hyperlink into the document
  11. TextControl1.HypertextLinks.Add(MyLink)
  12. End Sub
  1. // C#
  2. private void cmdInsertHyperlink_Click(object sender, System.EventArgs e)
  3. {
  4. // Create a HypertextLink object
  5. TXTextControl.HypertextLink MyLink =
  6. new TXTextControl.HypertextLink(
  7. "TX Text Control Web Site",
  8. "http://www.textcontrol.com");
  9.  
  10. // Insert the hyperlink into the document
  11. textControl1.HypertextLinks.Add(MyLink);
  12. }

Note that this first sample program does not handle the Click events, so clicking on a hypertext link in the TX Text Control will have no effect. Also, the link does not yet have the typical blue and underlined formatting style. Click events and formatting will be covered in Step 2.

TX Text Control's Save method is used to save the document as an HTML file:

  1. ' Visual Basic
  2. Private Sub cmdSaveAs_Click(ByVal sender As System.Object, _
  3. ByVal e As System.EventArgs) Handles cmdSaveAs.Click
  4. TextControl1.Save(TXTextControl.StreamType.HTMLFormat)
  5. End Sub
  1. // C#
  2. private void cmdSaveAs_Click(object sender, System.EventArgs e)
  3. {
  4. textControl1.Save (TXTextControl.StreamType.HTMLFormat);
  5. }

When the HTML file is opened in a web browser, the hypertext link is displayed as specified in your browser's settings.

Clicking on the link will take you to the TX Text Control web site.

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.

  1. ' Visual Basic
  2. Private Sub mnuInsert_Hyperlink_Click(ByVal sender As System.Object, _
  3. ByVal e As System.EventArgs) Handles mnuInsert_Hyperlink.Click
  4.  
  5. Dim Hyperlinks As New frmHyperlinks
  6. Hyperlinks.tx = TextControl1
  7. Hyperlinks.ShowDialog()
  8. HighlightHyperlinks(TextControl1, mnuView_Hyperlinks.Checked)
  9. End Sub
  1. // C#
  2. private void mnuInsert_Hyperlink_Click(object sender, System.EventArgs e)
  3. {
  4. frmHyperlinks Hyperlinks = new frmHyperlinks();
  5. Hyperlinks.tx = textControl1;
  6. Hyperlinks.ShowDialog();
  7. HighlightHyperlinks (textControl1, mnuView_Hyperlinks.Checked);
  8. }

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:

  1. ' Visual Basic
  2. Private Sub frmHyperlinks_Load(ByVal sender As System.Object, _
  3. ByVal e As System.EventArgs) Handles MyBase.Load
  4. Dim HyperLink As TXTextControl.HypertextLink = tx.HypertextLinks.GetItem()
  5.  
  6. If Not (HyperLink Is Nothing) Then
  7. ' If there is an existing hypertext link at the input position,
  8. ' copy its text and target to the text boxes on the form.
  9. txtLinkedText.Text = HyperLink.Text
  10. txtLinkTo.Text = HyperLink.Target
  11. Else
  12. ' If there is no hypertext link at the input position, but
  13. ' some text has been selected, then copy this text to the
  14. ' Linked Text text box.
  15. If (tx.Selection.Length > 0) Then
  16. txtLinkedText.Text = tx.Selection.Text
  17. End If
  18. End If
  19. End Sub
  1. // C#
  2. private void frmHyperlinks_Load(object sender, System.EventArgs e)
  3. {
  4. TXTextControl.HypertextLink HyperLink = tx.HypertextLinks.GetItem();
  5.  
  6. if (HyperLink != null)
  7. {
  8. // If there is an existing hypertext link at the input position,
  9. // copy its text and target to the text boxes on the form.
  10. txtLinkedText.Text = HyperLink.Text;
  11. txtLinkTo.Text = HyperLink.Target;
  12. }
  13. else
  14. {
  15. // If there is no hypertext link at the input position, but
  16. // some text has been selected, then copy this text to the
  17. // Linked Text text box.
  18. if (tx.Selection.Length > 0)
  19. txtLinkedText.Text = tx.Selection.Text;
  20. }
  21. }

The user can then 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:

  1. ' Visual Basic
  2. Private Sub cmdOK_Click(ByVal sender As System.Object, _
  3. ByVal e As System.EventArgs) Handles cmdOK.Click
  4.  
  5. Dim HyperLink As TXTextControl.HypertextLink = tx.HypertextLinks.GetItem()
  6.  
  7. If HyperLink Is Nothing Then
  8. ' Insert a new link
  9. Dim NewLink As New TXTextControl.HypertextLink( _
  10. txtLinkedText.Text, txtLinkTo.Text)
  11. tx.HypertextLinks.Add(NewLink)
  12. Else
  13. ' Update an existing link
  14. HyperLink.Text = txtLinkedText.Text
  15. HyperLink.Target = txtLinkTo.Text
  16. End If
  17.  
  18. Close()
  19. End Sub
  1. // C#
  2. private void cmdOK_Click(object sender, System.EventArgs e)
  3. {
  4. TXTextControl.HypertextLink HyperLink = tx.HypertextLinks.GetItem();
  5.  
  6. if (HyperLink == null)
  7. {
  8. // Insert a new link
  9. TXTextControl.HypertextLink NewLink =
  10. new TXTextControl.HypertextLink(
  11. txtLinkedText.Text , txtLinkTo.Text);
  12. tx.HypertextLinks.Add(NewLink);
  13. }
  14. else
  15. {
  16. // Update an existing link
  17. HyperLink.Text = txtLinkedText.Text;
  18. HyperLink.Target = txtLinkTo.Text;
  19. }
  20.  
  21. Close();
  22. }

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 TX Text Control method, but part of the sample program. It changes each element of TX Text Control's HyperLinks collection to the desired display style.

  1. ' Visual Basic
  2. Private Sub HighlightHyperlinks(ByVal tx As TXTextControl.TextControl, _
  3. ByVal Highlight As Boolean)
  4. Dim PreviousStart As Integer = tx.Selection.Start
  5. Dim PreviousLength As Integer = tx.Selection.Length
  6.  
  7. For Each Link As TXTextControl.HypertextLink In tx.HypertextLinks
  8. tx.Selection.Start = Link.Start - 1
  9. tx.Selection.Length = Link.Length
  10. If Highlight Then
  11. tx.Selection.ForeColor = Color.Blue
  12. tx.Selection.Underline = TXTextControl.FontUnderlineStyle.Single
  13. Else
  14. tx.Selection.ForeColor = Color.Black
  15. tx.Selection.Underline = TXTextControl.FontUnderlineStyle.None
  16. End If
  17. tx.Selection.Start = PreviousStart
  18. tx.Selection.Length = PreviousLength
  19. Next
  20. End Sub
  1. // C#
  2. void HighlightHyperlinks(TXTextControl.TextControl tx, bool bHighlight)
  3. {
  4. int PreviousStart = tx.Selection.Start, PreviousLength = tx.Selection.Length;
  5.  
  6. foreach (TXTextControl.HypertextLink Link in tx.HypertextLinks)
  7. {
  8. tx.Selection.Start = Link.Start-1;
  9. tx.Selection.Length = Link.Length;
  10. if (bHighlight)
  11. {
  12. tx.Selection.ForeColor = Color.Blue;
  13. tx.Selection.Underline = TXTextControl.FontUnderlineStyle.Single;
  14. }
  15. else
  16. {
  17. tx.Selection.ForeColor = Color.Black;
  18. tx.Selection.Underline = TXTextControl.FontUnderlineStyle.None;
  19. }
  20. }
  21. tx.Selection.Start = PreviousStart;
  22. tx.Selection.Length = PreviousLength;
  23. }

Step 3: Adding Document Targets

Step 1 and 2 introduced references to external resources, i.e. addresses of web pages or files. In this step, links to positions in the same document will be discussed. These links are called document links and the positions, to where they point, are called document targets. Document targets are also referred to as anchors (in the context of HTML editors) or bookmarks (in word processors).

When running this sample program, first add some text and then one or two targets with the Insert / Target... menu item.

Finally use the Insert / Hypertext Link... menu item to add links to these targets. The names of the targets you have inserted will be displayed in the Link Location combo box.

Inserting a Target

A target is created by adding a DocumentTargets Collection.

Unlike links, targets have no visible text. The constructor therefore has only a single parameter, which is the target's name.

  1. ' Visual Basic
  2. Dim Target As New TXTextControl.DocumentTarget(InsertTarget.TargetName)
  3. TextControl1.DocumentTargets.Add(Target)
  1. // C#
  2. TXTextControl.DocumentTarget Target =
  3. new TXTextControl.DocumentTarget(InsertTarget.TargetName);
  4. textControl1.DocumentTargets.Add(Target);

Inserting Links to Targets

The Hypertext Link dialog box from Step 2 needs to be extended so that it will not only accept URLs as link targets, but also let us select from a list of document targets. This is done by replacing the Link To text box with a combo box. The combo box is filled with targets by adding all elements from TX Text Control's DocumentTargets collection:

  1. ' Visual Basic
  2. For Each Target As TXTextControl.DocumentTarget In tx.DocumentTargets
  3. cboLinkTo.Items.Add("#" + Target.TargetName)
  4. Next
  1. // C#
  2. foreach (TXTextControl.DocumentTarget Target in tx.DocumentTargets)
  3. cboLinkTo.Items.Add("#" + Target.TargetName);

Jumping to a Target

When a document link is clicked, text should automatically be scrolled so that the text part containing the link's target becomes visible. This is done by responding to TX Text Control's TextField.

  1. ' Visual Basic
  2. Private Sub mnuView_JumpToTargets_Click(ByVal sender As System.Object, _
  3. ByVal e As System.EventArgs) Handles mnuView_JumpToTargets.Click
  4. e.DocumentLink.DocumentTarget.ScrollTo()
  5. End Sub
  1. // C#
  2. private void textControl1_DocumentLinkClicked(object sender, _
  3. TXTextControl.DocumentLinkEventArgs e)
  4. {
  5. e.DocumentLink.DocumentTarget.ScrollTo();
  6. }

Editing Links to Targets

The autoscroll feature has one drawback in that you can no longer click on a link and open the hyperlinks dialog box to edit it. This is because each time you click on a link, TX Text Control will scroll it out of view. However, this is fixed by adding a Jump to targets item to the View menu, which switches the autoscroll feature on and off as required.

Displaying and Editing Targets

Unlike HTML editors, TX Text Control does not display an icon to visualize the location of a target. But if targets are invisible, how can you locate them in your document, or delete one that's no longer required? The solution is a list box which displays the names of all targets a document contains, and lets you jump to or remove a selected target.

External verification page for ISO 9000:2000 certificate
ISO 9001:2000
certified