An interesting feature of the RichTextBox control is the automatic formatting of URLs embedded in the text. To enable this feature, set the DetectURLs property to True. Then, as soon as the control determines that you’re entering a URL (usually after you enter the three w’s and the following period), it will format the text as a hyperlink. When the pointer rests over a hyperlink, its shape turns into a hand, just as it would in Internet Explorer. Run the RichTextPad project, enter a URL such as http://www.w3computing.com, and see how the RichTextBox control handles it.
In addition to formatting the URL, the RichTextBox control triggers the LinkClicked event when a hyperlink is clicked. To display the corresponding page from within your code, enter the following statement in the LinkClicked event handler:
Private Sub RichTextBox1 LinkClicked( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.LinkClickedEventArgs) _
Handles RichTextBox1.LinkClicked
System.Diagnostics.Process.Start(e.LinkText)
End Sub
Code language: VB.NET (vbnet)
The System.Diagnostics.Process class provides the Start method, which starts an application. You can specify either the name of the executable or the path of a file. If you specify, the Start method will look up the associated application and start it. As you can see, handling embedded URLs with the RichTextBox control is almost trivial.
Displaying a Formatted Directory Listing
This is a good point to review the subroutines that produced the formatted directory listings shown in Figure 4.15 in the section “VB.NET FolderBrowserDialog Control“. Folder names are printed in bold by the PrintFolderName() subroutine, and filenames are printed in regular style by the PrintFileNames() subroutine. Both subroutines accept as arguments a DirectoryInfo object that represents the folder whose name (or files) we want to print, as well as an indentation string. This string is increased every time the code drills down to a subfolder and is decreased every time it moves up to a parent folder. Listing 4.21 shows the implementation of the two subroutines.
Listing 4.21: The PrintFolderName() and PrintFileNames() Subroutines
Private Sub PrintFolderName( _
ByVal folder As IO.DirectoryInfo, _
ByVal Indentation As String)
SwitchToBold()
RichTextBox1.AppendText(Indentation)
RichTextBox1.AppendText(folder.Name & vbCrLf)
SwitchToRegular()
End Sub
Private Sub PrintFileNames( _
ByVal folder As IO.DirectoryInfo, _
ByVal indentation As String)
Dim file As IO.FileInfo
For Each file In folder.GetFiles(”*.*”)
RichTextBox1.AppendText( _
indentation & file.Name & vbCrLf)
Next
End Sub
Code language: VB.NET (vbnet)
The code for printing folder names and filenames is trivial. Before calling the AppendText method to add a new folder name to the control, the code calls the SwitchToBold() subroutine. After printing the folder name, it calls the SwitchToRegular subroutine to reset the font. The two subroutines manipulate the SelectionFont property. Because no text is selected at the time, the subroutines simply change the attributes of the text that will be appended to the control with the next call to the AppendText method. The implementation of the two subroutines is shown next:
Private Sub SwitchToItalics()
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont.Name, _
RichTextBox1.SelectionFont.Size, FontStyle.Italic)
End Sub
Private Sub SwitchToRegular()
RichTextBox1.SelectionFont = _
New Font(RichTextBox1.SelectionFont.Name, _
RichTextBox1.SelectionFont.Size, FontStyle.Regular)
End Sub
Code language: VB.NET (vbnet)