Step 3 - Adding Targets

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:

Targets are realized again as a special type of a marked text field. The type and the target's name must be set with the FieldType and the FieldTypeData properties.

Unlike links, targets have no visible text, therefore an empty field must be inserted with the FieldInsert method to insert a target:

[Visual Basic 6]
Dim TargetName As String
TargetName = InputBox("Target name:", "Insert target")
If TargetName <> "" Then
   TXTextControl1.FieldInsert ""
   TXTextControl1.FieldType(TXTextControl1.FieldCurrent) = _
   TXTextControl1.FieldTypeData( _
      TXTextControl1.FieldCurrent) = TargetName
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 FieldNext method is used to fill this list box:

[Visual Basic 6]
Dim FieldID As Integer
FieldID = tx.FieldNext(0, &H100&)
While FieldID <> 0
   List1.AddItem tx.FieldTypeData(FieldID)
   FieldID = tx.FieldNext(FieldID, &H100&)

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 FieldGoto method to jump to the target:

[Visual Basic 6]
Private Sub TXTextControl1_FieldLinkClicked( _
      ByVal FieldIdAs Integer, _
      ByVal FieldType As Integer, _
      TypeData As String)
   TXTextControl1.FieldGoto txFieldLinkTarget, TypeData
End Sub

While the FieldGoto 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.