LinkLabel
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Use the LinkLabel control to add Web-style links to Windows Forms applications |
Contents |
A LinkLabel is a label control that can display hyperlinks that work in the same manner as web page links. They can open a web page or perform some other action when clicked. Multiple hyperlinks can be specified in the text of the control. Each hyperlink can perform a different task within an application.
Inheritance Hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.Label
System.Windows.Forms.LinkLabel
Useful properties
A LinkLabel has the same properties as a standard Label control as well as the following:
-
ActiveLinkColor- Defines the colour used to display an active link.
-
-
DisabledLinkColor- Defines the colour used when displaying a disabled link.
-
-
LinkArea- Defines the range in the text to treat as a link.
-
-
LinkBehavior- Defines a value that represents the behavior of a link. Takes a value from theLinkBehaviorenumeration:
-
-
AlwaysUnderline- The link always displays with underlined text. -
HoverUnderline- The link displays underlined text only when the mouse is hovered over the link text. -
NeverUnderline- The link text is never underlined. The link can still be distinguished from other text by use of theLinkColorproperty of theLinkLabelcontrol. -
SystemDefault- The behavior of this setting depends on the options set using the Internet Options dialog box in Control Panel or Internet Explorer.
-
-
LinkColor- Defines the colour used when displaying a normal link.
-
-
Links- Defines the collection of links contained within theLinkLabel.
-
-
LinkVisited- Defines a value indicating whether a link should be displayed as though it were visited.
-
-
VisitedLinkColor- Defines the colour used when displaying a link that that has been previously visited.
-
Link Colour
The LinkColor defines the colour of the link before it has been clicked on. The ActiveLinkColor defines the colour of the link when it is being clicked on. The VisitedLinkColor defines the colour of the link when it has been clicked on.
In order for the VisitedLinkColor to be used, the LinkVisited property must be set to true.
Link Behaviour
The LinkBehavior property defines how the link reacts. When set to SystemDefault, the LinkLabel will display in the way defined by the System. This can be overridden by setting this property to one of the other three values. AlwaysUnderline and NeverUnderline are self explanatory, while HoverUnderline will only underline the text when the mouse hovers over the text.
Adding a Link
Each hyperlink in a LinkLabel is an instance of the LinkLabel.Link class. The LinkLabel.Link class defines display information, state, and location of the hyperlink. The LinkLabel stores these links in the LinkLabel.LinkCollection class instance for the control named Links.
There are two ways to add a hyperlink to the LinkLabel control. The quickest way is to specify a LinkArea and assign it to the LinkArea property. This enables you to specify a single hyperlink within the Text of the control. A LinkArea is specified by two parameters, start and length:
-
start- The zero-based starting location of the link area within the text of theLinkLabel.
-
-
length- The number of characters, after the starting character, to include in the link area.
-
this.linkLabel1.LinkArea = new LinkArea(0, 8); this.linkLabel1.Links[0].LinkData = this.linkLabel1.Text.Substring(0, 8);
So the example above uses the first 8 characters of the Text property of the LinkLabel as the hyperlink, which are registered as the data to link to also.
Adding multiple Links
To add multiple hyperlinks, you can use the Add method of the LinkLabel.LinkCollection class by accessing the collection through the Links property.
this.linkLabel1.Links.Add(24, 9, "www.microsoft.com");
The Add method is specifed with three parameters, start, length and linkData.
-
start- The zero-based starting location of the link area within the text of theLinkLabel.
-
-
length- The number of characters, after the starting character, to include in the link area.
-
-
linkData- The object containing the information to associate with the link.
-
Linking to a Web Page
By specifying the LinkData as a string to a web site such as "www.microsoft.com", a process can be started that opens the web site in the default browser in the clicked event.
string target = e.Link.LinkData as string; System.Diagnostics.Process.Start(target);
Add a LinkLabel to a Form manually
The MSDN page for the LinkLabel control has the following example showing how to add a LinkLabel to a Form.
private LinkLabel linkLabel1; public Form1() { CreateMyLinkLabel(); } public void CreateMyLinkLabel() { // Create the LinkLabel. linkLabel1 = new LinkLabel(); // Configure the LinkLabel's size and location. Specify that the // size should be automatically determined by the content. this.linkLabel1.Location = new System.Drawing.Point(34, 56); this.linkLabel1.Size = new System.Drawing.Size(224, 16); this.linkLabel1.AutoSize = true; // Configure the appearance. // Set the DisabledLinkColor so that a disabled link will show up against // the form's background. this.linkLabel1.DisabledLinkColor = System.Drawing.Color.Red; this.linkLabel1.VisitedLinkColor = System.Drawing.Color.Blue; this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline; this.linkLabel1.LinkColor = System.Drawing.Color.Navy; this.linkLabel1.TabIndex = 0; this.linkLabel1.TabStop = true; // Add an event handler to do something when the links are clicked. this.linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked); // Identify what the first Link is. this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 8); // Identify that the first link is visited already. this.linkLabel1.Links[0].Visited = true; // Set the Text property to a string. this.linkLabel1.Text = "Register Online. Visit Microsoft. Visit MSN."; // Create new links using the Add method of the LinkCollection class. // Underline the appropriate words in the LinkLabel's Text property. // The words 'Register', 'Microsoft', and 'MSN' will // all be underlined and behave as hyperlinks. // First check that the Text property is long enough to accommodate // the desired hyperlinked areas. If it's not, don't add hyperlinks. if (this.linkLabel1.Text.Length >= 45) { this.linkLabel1.Links[0].LinkData = this.linkLabel1.Text.Substring(0, 8); this.linkLabel1.Links.Add(24, 9, "www.microsoft.com"); this.linkLabel1.Links.Add(42, 3, "www.msn.com"); // The second link is disabled and will appear as red. this.linkLabel1.Links[1].Enabled = false; } this.Controls.Add(linkLabel1); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // Determine which link was clicked within the LinkLabel. this.linkLabel1.Links[linkLabel1.Links.IndexOf(e.Link)].Visited = true; // Display the appropriate link based on the value of the // LinkData property of the Link object. string target = e.Link.LinkData as string; // If the value looks like a URL, navigate to it. // Otherwise, display it in a message box. if (null != target && target.StartsWith("www")) { System.Diagnostics.Process.Start(target); } else { MessageBox.Show("Item clicked: " + target); } }
MSDN references
|

