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 (see Fig. 14.13). Class LinkLabel is derived from class Label and therefore inherits all of class Label's functionality.

Figure 14.12. LinkLabel control in running program.

Figure 14.13. LinkLabel properties and an event.

(This item is displayed on page 664 in the print version)

LinkLabel properties and event

Description

Common Properties

ActiveLinkColor

Specifies the color of the active link when clicked.

LinkArea

Specifies which portion of text in the LinkLabel is part of 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 in the Text property acts as 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 arguments LinkLabelLinkClickedEventArgs)

LinkClicked

Generated when the link is clicked. This is the default event when the control is double clicked in Design mode.

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 LinkLabelTestForm (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 driveLinkLabel, deitelLinkLabel and notepadLinkLabel 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: LinkLabelTestForm.cs 2 // Using LinkLabels to create hyperlinks. 3 using System; 4 using System.Windows.Forms; 5 6 // Form using LinkLabels to browse the C:drive, 7 // load a webpage and run Notepad 8 public partial class LinkLabelTestForm : Form 9 { 10 // default constructor 11 public LinkLabelTestForm() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // browse C:drive 17 private void driveLinkLabel_LinkClicked( object sender, 18 LinkLabelLinkClickedEventArgs e ) 19 { 20 // change LinkColor after it has been clicked 21 driveLinkLabel.LinkVisited = true; 22 23 System.Diagnostics.Process.Start( @"C:" ); 24 } // end method driveLinkLabel_LinkClicked 25 26 // load www.deitel.com in web browser 27 private void deitelLinkLabel_LinkClicked( object sender, 28 LinkLabelLinkClickedEventArgs e ) 29 { 30 // change LinkColor after it has been clicked 31 deitelLinkLabel.LinkVisited = true; 32 33 System.Diagnostics.Process.Start( 34 "IExplore", "http://www.deitel.com" ); 35 } // end method deitelLinkLabel_LinkClicked 36 37 // run application Notepad 38 private void notepadLinkLabel_LinkClicked( object sender, 39 LinkLabelLinkClickedEventArgs e ) 40 { 41 // change LinkColor after it has been clicked 42 notepadLinkLabel.LinkVisited = true; 43 44 // program called as if in run 45 // menu and full path not needed 46 System.Diagnostics.Process.Start( "notepad" ); 47 } // end method driveLinkLabel_LinkClicked 48 } // end class LinkLabelTestForm

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, full path names are not needed, 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 driveLinkLabel's LinkClicked event browses the C: drive (lines 1724). Line 21 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 23), which opens a Windows Explorer window. The @ symbol that we placed before "C:" indicates that all characters in the string should be interpreted literally. Thus, the backslash within the string is not considered to be the first character of an escape sequence. This simplifies strings that represent directory paths, since you do not need to use \ for each character in the path.

The event handler for deitelLinkLabel's LinkClicked event (lines 2735) opens the Web page www.deitel.com in Internet Explorer. We achieve this by passing the Web page address as a string (lines 3334), which opens Internet Explorer. Line 31 sets the LinkVisited property to TRue.

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

ListBox Control

Категории