Step 3 - Adding Targets

Visual Basic User's Guide > Hypertext Links

Step 1 and 2 only handle references to external resources, i.e. addresses of web pages or files. In this step, links to positions in the same document will be handled. These links are called internal links and the positions, to where they point, are called targets. Targets are also referred to as anchors (in the context of HTML editors) or bookmarks (in word processors). When using this example, first add some text and then some targets with the Insert / Target... menu item. Finally use the Insert / Hypertext Link... menu item to add links to these targets.

1. Inserting a Target:

Use the TargetInsert method to insert a target:

[Visual Basic 6]
Dim targetName As String
targetName = InputBox("Target name:", "Insert target")
If targetName <> "" Then
    TXTextControl1.TargetInsert targetName, True
End If

Only one text box is required to display the name of a target, so a simple InputBox statement can be used.

Inserting Links to Targets

To insert links to the just inserted targets, the Hypertext Link dialog box is extended with a list box showing the names of all targets the document contains. The TargetNext method is used to fill this list box:

[Visual Basic 6]
targetId As Integer
List1.Clear
targetId = tx.TargetNext(0)
While targetId <> 0
    List1.AddItem tx.targetName(targetId)
    targetId = tx.TargetNext(targetId)
Wend

When the user selects a target, the Link To field is filled with the target's name.

After typing the link's text and pressing the OK button, the link is inserted. An internal link is inserted in the same way as the external links from step 1, but the FieldType property now is set to txFieldInternalLink and the FieldTypeData property is set to the target's name.

2. Jumping to a Target:

After inserting internal links and targets, a jump must be realized. When the user clicks on a marked text field that represents a hypertext link, Text Control fires a FieldLinkClicked event. The information provided through this event can be used with the TargetGoto method to jump to the target:

[Visual Basic 6]
Private Sub TXTextControl1_FieldLinkClicked( _
      ByVal FieldId As Integer, _
      ByVal FieldType As Integer, _
      TypeData As String)
   If FieldType = txFieldInternalLink Then
      TXTextControl1.TargetGoto TypeData
   End If
End Sub

While the TargetGoto method is used for targets within the same file, links to external targets must be treated differently. When the Jumping to an External Target event occurs, and the FieldType parameter indicates that the link is external, then it depends on the type of the application, what to do. External links can point to, for instance, files on the local harddisk, or addresses in the internet.

Note that responding to the events is only required for making the hypertext links work while the text is edited in Text Control. If the text is saved to a file and displayed with a browser, then the hypertext links will work depending on the used browser.