TX Text Control ActiveX

Hypertext Links

Topic

Hypertext links in a TXTextControl document are marked text fields of a special type, and so are the targets they jump to. In four steps, a Visual Basic 6 program grows from a single inserted link to a small hypertext editor: it inserts and edits links in a dialog box, adds targets, and responds to the events that TX Text Control fires when the user clicks a link, including jumps to targets in other files.

The source code of the steps is in the subfolders Step1 to Step4 of the Samples\vb6\Hyperlnk sample source directory. Each step extends the program of the one before.

Add Targets

Steps 1 and 2 only handle references to external resources, the addresses of web pages or files. The third program adds internal links, which point to targets in the same document. To try it, type some text, add some targets with the Insert / Target... menu item, and then add links to them with Insert / Hypertext Link....

Insert a Target

The Insert target dialog box with an empty Target name box and the buttons OK and Cancel.

The TargetInsert method inserts a target:

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

A target needs only one text box for its name, so a plain InputBox statement is enough. The second parameter of TargetInsert, True, lets the user delete the target while editing the document.

Link to a Target

To insert links to these targets, the program extends the Hypertext Link dialog box with a list box that shows the names of all targets in the document. The TargetNext method fills it:

Dim targetId As Integer
List1.Clear
targetId = tx.TargetNext(0)
While targetId <> 0
    List1.AddItem tx.targetName(targetId)
    targetId = tx.TargetNext(targetId)
Wend

TargetNext returns the identifier of the next target, the first one for 0 and 0 after the last one, and TargetName returns its name. When the user selects a target, the Link To box is filled with the target's name.

The Hypertext Link dialog box of step 3 with Linked text set to Goto 1, Link to set to target1 and a Targets list with target1 and target2.

After the user types the text of the link and clicks OK, the link is inserted. An internal link is inserted the same way as the external link of step 1, but FieldType is set to txFieldInternalLink and FieldTypeData to the name of the target.

Jump to a Target

With internal links and targets in place, the program has to carry out the jump. When the user clicks a marked text field that represents a hypertext link, TX Text Control fires a FieldLinkClicked event. The TargetGoto method uses the information the event provides to jump to the target:

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

TargetGoto handles targets in the same file. Links to external targets need different treatment: when FieldLinkClicked occurs and its FieldType parameter says that the link is external, what to do depends on the type of application. External links can point to files on the local hard disk or to addresses on the internet, for example.

The events only have to be handled to make the links work while the text is edited in TXTextControl. When the text is saved to a file and displayed in a browser, the links work as the browser supports them.

Add Jumps to External Targets

The fourth program adds jumps to other documents and to targets in these documents.

Choose a File and Its Targets

The program extends the Hypertext Link dialog box again, this time to choose an external file. A Choose File... button on the form opens a common dialog.

The Hypertext Link dialog box of step 4 with Linked text, a Link to box with a file path and target, a Choose file button, a list of named targets and the options Current page and Selected file under Show targets in.

After the user chooses a file, the text box displays its name and the program searches the file for internal targets:

Private Sub CheckFileForTargets(file As String)
   tx(1).LoadSaveAttribute(txEnableLinks) = True
   tx(1).Load file, 0, 4
   FillListboxWithTargets (1)
   optSelFile.Value = True
   loadedFile = file
End Sub

The program loads the file into a second, invisible TXTextControl and then lists all of its targets in the same way as in step 3.

Jump to a Target in Another File

To jump to an external link, the program extends the FieldLinkClicked handler of step 3. The following code doesn't handle jumps to internet addresses; it only jumps to targets in other files. TX Text Control separates the name of a file from the name of a target with the # character. The code splits the two, loads the file with the Load method and jumps to the target with TargetGoto:

ElseIf FieldType = txFieldExternalLink Then
   'This sample does not feature links to www sites,
   'so exit sub
   If Left(TypeData, 7) = "http://" Then
      Exit Sub
   End If
   TXTextControl1.LoadSaveAttribute(txEnableLinks) = True
   pos = InStr(TypeData, "#")
   'File name includes an internal link
   If pos > 0 Then
      'Ask the user to save changes, if any
      If bDocDirty Then
      ret = MsgBox("Save changes?", vbYesNoCancel, _
               "Question")
         If ret = vbYes Then
            mnuFile_SaveAs_Click
         ElseIf ret = vbCancel Then
            Exit Sub
         End If
   End If
      'Extract file name & path from full path
   str = Left(TypeData, pos - 1)
      TXTextControl1.Load str, 0, 4
      'Extract file position from full path
      str = Mid(TypeData, pos + 1)
      TXTextControl1.TargetGoto str
   Else
      'File name doesn't include an internal link
      TXTextControl1.Load TypeData, 0, 4
   End If
End If

The code works as follows:

  • An address that starts with http:// ends the procedure.
  • If the address contains #, the program asks the user to save changes, if there are any, loads the file named before the # and jumps to the target named after it.
  • An address without a target only loads the file. The third parameter of Load, 4, is the HTML format.

Next Steps