The LinkLabel control displays a label that is associated with a hyperlink. By default, the label is blue and underlined, so it is easy to recognize as a link. It also displays a pointing hand cursor when the mouse moves over it, so it looks more or less like a link on a web page.
When the user clicks a LinkLabel, the control raises its LinkClicked event. The program can catch the event and take whatever action is appropriate. For example, it could display another form, open a document, or open a web page.
The following code shows how a program might display a web page with a URL stored in the LinkLabel control’s Tag property. The call to System.Diagnostics.Process.Start makes the operating system perform the default action for the string value stored in the Tag property. If that value is a URL, the system will normally open the URL with the system’s default browser.
Private Sub llblBookUrl_LinkClicked(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles llblBookUrl.LinkClicked System.Diagnostics.Process.Start(llblBookUrl.Tag.ToString) End Sub
The LinkLabel control provides all the formatting properties that the Label control does. See the section “Label” earlier in this appendix for more information on formatting the control’s label.
The control also provides several properties for determining the appearance of its link. The following table describes some of the most useful.
Property | Purpose |
---|---|
ActiveLinkColor | The color of an active link. |
DisabledLinkColor | The color of a disabled link. |
LinkArea | The piece of the control’s text that is represented as a link. This includes a start position and a length in characters. |
LinkBehavior | Determines when the link is underlined. This can take the values AlwaysUnderline, HoverUnderline, NeverUnderline, and SystemDefault. |
LinkColor | The color of a normal link. |
Links | A collection of objects representing the link(s) within the control’s text. |
LinkVisited | A Boolean that indicates whether the link should be displayed as visited. |
VisitedLinkColor | The color of a visited link. |
ALinkLabel can display more than one link within its text. For example, in the text “The quick brown fox jumps over the lazy dog,” the control might display the words fox and dog as links and the rest as normal text. At design time, you can use the LinkArea property to specify only one link. To make fox the link, the program would set LinkArea to 16, 3 to start with letter 16 and include 3 letters.
At runtime, the program can add other links to the control. The following code clears the llblPangram control’s Links collection and then adds two new link areas. The program sets each of the new Link objects’ LinkData property to a URL that the program should display when the user clicks that link.
Dim new_link As System.Windows.Forms.LinkLabel.Link llblPangram.Links.Clear() new_link = llblPangram.Links.Add(16, 3) new_link.LinkData = "http://www.somewhere.com/fox.htm" new_link = llblPangram.Links.Add(40, 3) new_link.LinkData = "http://www.somewhere.com/dog.htm"
The following code shows how the program displays the URL corresponding to the link the user clicked. The code gets the appropriate Link object from the event handler’s event arguments, converts the Link object’s LinkData property into a string, and uses System.Diagnostics.Process .Start to open it with the system’s default browser.
' Display the URL associated with the clicked link. Private Sub llblPangram_LinkClicked(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles llblPangram.LinkClicked System.Diagnostics.Process.Start(e.Link.LinkData.ToString) End Sub