14.5. LinkLabel ControlThe 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.
Look-and-Feel Observation 14.3
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.
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. |