Step 2 - Adding a Dialog Box for Inserting 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 "".

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 property, tx is used to pass a Text Control's reference to the form.

procedure TForm1.HypertextLink2Click(Sender: TObject);
   Form2.tx := TXTextControl1;

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:

procedure TForm2.FormShow(Sender: TObject);
   // If the caret is inside an existing hyperlink,
   // copy the hyperlink's text and link information
   // to the text boxes on the form.
   If (Form1.TXTextControl1.FieldAtInputPos <> 0) Then  begin
      txtLinkedText.Text := Form1.TXTextControl1.FieldText;
      txtLinkTarget.Text :=
   end Else begin
      txtLinkedText.Text := Form1.TXTextControl1.SelText;
      txtLinkTarget.Text := '';

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:

If tx.FieldAtInputPos <> 0 Then begin
   // editing an existing hyperlink
   tx.FieldText := txtLinkedText.Text;
   tx.FieldType[tx.FieldCurrent] := txFieldExternalLink;
   tx.FieldTypeData[tx.FieldCurrent] := txtLinkTarget.Text;
end Else begin
   // insert new hyperlink
   tx.FieldInsert (txtLinkedText.Text);
   tx.FieldType[tx.FieldCurrent] := txFieldExternalLink;
   tx.FieldTypeData[tx.FieldCurrent] := txtLinkTarget.Text;
   HighlightHyperlinks (tx, Form1.HypertextLinks1.Checked);

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 Unit3.pas.