LinkLabel
ControlThe 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.
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.
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 LinkLabel
s to link to the C:
drive, the Deitel website (www.deitel.com
) and the Notepad app, respectively. The Text
proper-ties of the LinkLabel
s cDriveLinkLabel
, deitelLinkLabel
and notepadLinkLabel
describe each link’s purpose.
The event handlers for the LinkLabel
s 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 string
s 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.