Visual Basic 2005 for Programmers (2nd Edition)

14.5. LinkLabel Control

The LinkLabel control displays links to other resources, such as files or Web pages (Fig. 14.12). A LinkLabel appears as underlined text (colored blue by default). When the mouse moves over the link, the pointer changes to a hand; this is similar to the behavior of a hyperlink in a Web page. The link can change color to indicate whether the link is new, previously visited or active. When clicked, the LinkLabel generates a LinkClicked event. Class LinkLabel is derived from class Label and therefore inherits all of class Label's functionality. Figure 14.13 lists several LinkLabel properties and a common event.

Figure 14.12. LinkLabel control in running program.

Figure 14.13. LinkLable prorperties and an event.

LinkLabelproperties and an event

Descripiont

Common Properties

ActiveLinkColor

Specifies the color of the link when clicked.

LinkArea

Specifies which portion of text in the LinkLabel is partof the link.

LinkBehavior

Specifies the link's behavior, such as how the link appears when the mouse is placed over it.

LinkColor

Specifies the original color of all links before they have been visited. The default color is set by the system, but is usually blue.

LinkVisited

If true, the link appears as though it has been visited (its color is changed to that specified by property VisitedLinkColor). The default value is False.

Text

Specifies the control's text.

UseMnemonic

If TRue, the & character can be used in the Text property to create a shortcut (similar to the Alt shortcut in menus).

VisitedLinkColor

Specifies the color of visited links. The default color is set by the system, but is usually purple.

Common Event (event argument type is LinkLabelLinkClickedEventArgs)

LinkClicked

Generated when the link is clicked.

Look-and-Feel Observation 14.3

A LinkLabel is the preferred control for indicating that the user can click a link to jump to a resource such as a Web page, though other controls can perform similar tasks.

Class FrmLinkLabelTest (Fig. 14.14) uses three LinkLabels to link to the C: drive, the Deitel Web site (www.deitel.com) and the Notepad application, respectively. The Text properties of the LinkLabel's lnklblCDrive, lnklblDeitel and lnklblNotepad describe each link's purpose.

Figure 14.14. LinkLabels used to link to a drive, a Web page and an application.

1 ' Fig. 14.14: FrmLinkLabelTest.vb 2 ' Using LinkLabels to create hyperlinks. 3 Public Class FrmLinkLabelTest 4 ' browse C:\ drive 5 Private Sub lnklblCDrive_LinkClicked(ByVal sender As System.Object, _ 6 ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ 7 Handles lnklblCDrive.LinkClicked 8 9 lnklblCDrive.LinkVisited = True ' change LinkColor after click 10 System.Diagnostics.Process.Start( "C:\" ) 11 End Sub ' lnklblCDrive_LinkClicked 12 13 ' browse www.deitel.com in Internet Explorer 14 Private Sub lnklblDeitel_LinkClicked(ByVal sender As System.Object, _ 15 ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ 16 Handles lnklblDeitel.LinkClicked 17 18 lnklblDeitel.LinkVisited = True ' change LinkColor after click 19 System.Diagnostics.Process.Start( _ 20 "IExplore", "http://www.deitel.com") 21 End Sub ' lnklblDeitel_LinkClicked 22 23 ' run the Notepad application 24 Private Sub lnklblNotepad_LinkClicked(ByVal sender As System.Object, _ 25 ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ 26 Handles lnklblNotepad.LinkClicked 27 28 lnklblNotepad.LinkVisited = True ' change LinkColor after click 29 System.Diagnostics.Process.Start("notepad") 30 End Sub ' lnklblNotepad_LinkClicked 31 End Class ' FrmLinkLabelTest

The event handlers for the LinkLabels call method Start of class Process (namespace System.Diagnostics), which allows you to execute other programs from an application. Method Start can take one argument, the file to open (a String), or two arguments, the application to run and its command-line arguments (two Strings). Method Start's arguments can be in the same form as if they were provided for input to the Windows Run command (Start > Run...). For applications that are known to Windows (such as Notepad), full path names are not required, and the .exe extension often can be omitted. To open a file that has a file type that Windows recognizes, simply use the file's full path name. The Windows operating system must be able to use the application associated with the given file's extension to open the file.

The event handler for lnklblCDrive's LinkClicked event browses the C: drive (lines 511). Line 9 sets the LinkVisited property to TRue, which changes the link's color from blue to purple (the LinkVisited colors can be configured through the Properties window in Visual Studio). The event handler then passes "C:\" to method Start (line 10), which opens a Windows Explorer window.

The event handler for lnklblDeitel's LinkClicked event (lines 1421) opens the Web page www.deitel.com in Internet Explorer. We achieve this by passing the Web page address as a String (lines 1920), which opens Internet Explorer. Line 18 sets the LinkVisited property to true.

The event handler for lnklblNotepad's LinkClicked event (lines 2430) opens the Notepad application. Line 28 sets the LinkVisited property to TRue so that the link appears as a visited link. Line 29 passes the argument "notepad" to method Start, which runs notepad.exe. Note that in line 29, the .exe extension is not requiredWindows can determine whether it recognizes the argument given to method Start as an executable file.

Категории