Step 2: Adding a Dialog Box for Inserting Hypertext Links

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

The dialog box contains 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 which the link points.

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

[C#]
wndHyperlink Hyperlinks = new wndHyperlink();
Hyperlinks.tx = textControl1;
Hyperlinks.ShowDialog();
HighlightHyperlinks(textControl1, mnuView.IsChecked);
[Visual Basic]
Dim Hyperlinks As New wndHyperlink()
Hyperlinks.tx = textControl1
Hyperlinks.ShowDialog()
HighlightHyperlinks(textControl1, mnuView.IsChecked)

When the form is loaded, the text boxes are filled with the text and link information, provided that the current input position is inside a link:

[C#]
private void Window_Loaded(object sender, RoutedEventArgs e)
{
	TXTextControl.HypertextLink HyperLink = tx.HypertextLinks.GetItem();

	if (HyperLink != null)
	{
		// If there is an existing hypertext link at the input position,
		// copy its text and target to the text boxes on the form.
		tbLinkedText.Text = HyperLink.Text;
		tbLinkTo.Text = HyperLink.Target;
	}
	else
	{
		// If there is no hypertext link at the input position, but
		// some text has been selected, then copy this text to the
		// Linked Text text box.
		if (tx.Selection.Length > 0)
			tbLinkedText.Text = tx.Selection.Text;
	}
}
[Visual Basic]
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
	Dim HyperLink As TXTextControl.HypertextLink = tx.HypertextLinks.GetItem()

	If HyperLink IsNot Nothing Then
		' If there is an existing hypertext link at the input position,
		' copy its text and target to the text boxes on the form.
		tbLinkedText.Text = HyperLink.Text
		tbLinkTo.Text = HyperLink.Target
	Else
		' If there is no hypertext link at the input position, but
		' some text has been selected, then copy this text to the
		' Linked Text text box.
		If tx.Selection.Length > 0 Then
			tbLinkedText.Text = tx.Selection.Text
		End If
	End If
End Sub

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

[C#]
private void btnOK_Click(object sender, RoutedEventArgs e)
{
	if (tbLinkedText.Text == "")
		MessageBox.Show("Hyperlink contains no text. Please enter the text to be shown as a hyperlink in the Linked Text field.", this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
	else if (tbLinkTo.Text == "")
		MessageBox.Show("No target specified. Please enter a target URL in the Link To field.", this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
	else
	{
		TXTextControl.HypertextLink HyperLink = tx.HypertextLinks.GetItem();
		if (HyperLink == null)
		{
			// Insert a new link
			TXTextControl.HypertextLink NewLink =
				new TXTextControl.HypertextLink(
				tbLinkedText.Text, tbLinkTo.Text);
			tx.HypertextLinks.Add(NewLink);
		}
		else
		{
			// Update an existing link
			HyperLink.Text = tbLinkedText.Text;
			HyperLink.Target = tbLinkTo.Text;
		}

		Close();
	}
}
[Visual Basic]
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
	If tbLinkedText.Text = "" Then
		MessageBox.Show("Hyperlink contains no text. Please enter the text to be shown as a hyperlink in the Linked Text field.", Me.Title, MessageBoxButton.OK, MessageBoxImage.[Error])
	ElseIf tbLinkTo.Text = "" Then
		MessageBox.Show("No target specified. Please enter a target URL in the Link To field.", Me.Title, MessageBoxButton.OK, MessageBoxImage.[Error])
	Else
		Dim HyperLink As TXTextControl.HypertextLink = tx.HypertextLinks.GetItem()
		If HyperLink Is Nothing Then
			' Insert a new link
			Dim NewLink As New TXTextControl.HypertextLink(tbLinkedText.Text, tbLinkTo.Text)
			tx.HypertextLinks.Add(NewLink)
		Else
			' Update an existing link
			HyperLink.Text = tbLinkedText.Text
			HyperLink.Target = tbLinkTo.Text
		End If

		Close()
	End If
End Sub

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 not a Text Control method, but part of the sample program. It changes each element of the Text Control's HypertextLinkCollection to the desired display style.

[C#]
void HighlightHyperlinks(TXTextControl.WPF.TextControl tx, bool Highlight)
{
	int PreviousStart = tx.Selection.Start, PreviousLength = tx.Selection.Length;

	foreach (TXTextControl.HypertextLink Link in tx.HypertextLinks)
	{
		tx.Selection.Start = Link.Start - 1;
		tx.Selection.Length = Link.Length;
		if (Highlight)
		{
			tx.Selection.ForeColor = System.Drawing.Color.Blue;
			tx.Selection.Underline = TXTextControl.FontUnderlineStyle.Single;
		}
		else
		{
			tx.Selection.ForeColor = System.Drawing.Color.Black;
			tx.Selection.Underline = TXTextControl.FontUnderlineStyle.None;
		}
	}
	tx.Selection.Start = PreviousStart;
	tx.Selection.Length = PreviousLength;
}
[Visual Basic]
Private Sub HighlightHyperlinks(ByVal tx As TXTextControl.WPF.TextControl, ByVal Highlight As Boolean)
	Dim PreviousStart As Integer = tx.Selection.Start, PreviousLength As Integer = tx.Selection.Length

	For Each Link As TXTextControl.HypertextLink In tx.HypertextLinks
		tx.Selection.Start = Link.Start - 1
		tx.Selection.Length = Link.Length
		If Highlight Then
			tx.Selection.ForeColor = System.Drawing.Color.Blue
			tx.Selection.Underline = TXTextControl.FontUnderlineStyle.[Single]
		Else
			tx.Selection.ForeColor = System.Drawing.Color.Black
			tx.Selection.Underline = TXTextControl.FontUnderlineStyle.None
		End If
	Next
	tx.Selection.Start = PreviousStart
	tx.Selection.Length = PreviousLength
End Sub