15.5 LinkLabel Control

The LinkLabel control displays links to other resources, such as files or web pages (Fig. 15.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 it is not yet visited, previously visited or active (the mouse is over the link and a button is pressed). When clicked, the LinkLabel generates a LinkClicked event (see Fig. 15.13). Class Link-Label is derived from class Label and therefore inherits all of class Label’s functionality.

Fig. 15.12 LinkLabel control in running program.

Look-and-Feel Observation 15.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.

Fig. 15.13 LinkLabel properties and an event.

LinkLabel properties and an event Description
Common Properties
ActiveLinkColor Specifies the color of the active link when the user is in the process of clicking the link. The default color (typically red) is set by the system.
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 the link before it’s been visited. The default color (typically blue) is set by the system.
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 a visited link. The default color (typically purple) is set by the system.
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.

Class LinkLabelTestForm (Fig. 15.14) uses three LinkLabels to link to the C: drive, the Deitel website (www.deitel.com) and the Notepad app, respectively. The Text proper-ties of the LinkLabels cDriveLinkLabel, deitelLinkLabel and notepadLinkLabel describe each link’s purpose.

Fig. 15.14 Using LinkLabels to create hyperlinks.

Alternate View

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

The event handlers for the LinkLabels call method Start of class Process (namespace System.Diagnostics), which allows you to execute other programs, or load documents or web sites from an app. Method Start can take one argument, the file to open, or two arguments, the app to run and its command-line arguments. 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 apps that are known to Windows, full path names are not needed, and the file extension often can be omitted. To open a file of a type that Windows recognizes (and knows how to handle), simply use the file’s full path name. For example, if you a pass the method a .docx file, Windows will open it in Microsoft Word (or whatever program is registered to open .docx files, if any). The Windows operating system must be able to use the app associated with the given file’s extension to open the file.

The event handler for cDriveLinkLabel’s LinkClicked event browses the C: drive (lines 19–26). Line 23 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 25), which opens a Windows Explorer window. The @ symbol that we placed before "C:" indicates that all characters in the string should be interpreted literally—this is known as a verbatim string. 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 29–36) opens the web page www.deitel.com in the user’s default web browser. We achieve this by passing the web-page address as a string (line 35), which opens the web page in a new web browser window or tab. Line 33 sets the LinkVisited property to true.

The event handler for notepadLinkLabel’s LinkClicked event (lines 39–48) opens the Notepad app. Line 43 sets the LinkVisited property to true so that the link appears as a visited link. Line 47 passes the argument "notepad" to method Start, which runs notepad.exe. In line 47, neither the full path nor the .exe extension is required—Windows automatically recognizes the argument given to method Start as an executable file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset