Step 2 - Adding a Dialog Box for Inserting Hypertext Links

Visual Basic User's Guide > Hypertext Links

In this second sample program a dialog box is created which enables the user 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 Text Control's proprietary format.

The dialog box has 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 where the link points. In the step 1 example, the representing text was "Text Control Web Site", and the address, to where the link points, was "http://www.textcontrol.com".

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 of an existing link, this link is modified. Otherwise a new one is inserted.

The dialog form's properties, i tx and bShowHyperlinks, are used to pass a Text Control's reference and some information about how to display the hypertext links to the form.

[Visual Basic 6]
Public Sub do_mnuInsert_Hyperlink_Click(tx As _
            TXTextControl, bShowHyperlinks As Boolean)
   Set frmHyperlink.tx = tx
   frmHyperlink.bShowHyperlinks = _
      (mnuView_Hyperlinks.Checked = True)
   frmHyperlink.Show 1
End Sub

When the form is loaded, the text boxes are filled with the text and link information when the current input position is inside of an existing link:

[Visual Basic 6]
Private Sub Form_Load()
   If CaretInsideHyperlink(tx) <> 0 Then
      txtLinkedText = tx.FieldText
      txtLinkTarget = tx.FieldTypeData(tx.FieldCurrent)
   Else
      txtLinkedText = tx.SelText
   End If
End Sub

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

[Visual Basic 6]
If tx.FieldAtInputPos <> 0 Then
   ' editing an existing hypertext link
   tx.FieldText = txtLinkedText
   tx.FieldType(tx.FieldCurrent) = txFieldExternalLink
   tx.FieldTypeData(tx.FieldCurrent) = txtLinkTarget
Else
   ' insert new hypertext link
   tx.FieldInsert txtLinkedText
   tx.FieldType(tx.FieldCurrent) = txFieldExternalLink
   tx.FieldTypeData(tx.FieldCurrent) = txtLinkTarget
   HighlightHyperlinks tx, bShowHyperlinks
End If

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 defined in the file HyperlinkFunctions.bas.