Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » Windows Controls C# » Understanding and Using the LinkLabel Control

Understanding and Using the LinkLabel Control

In this article, I will discuss some functionality related to the LinkLabel control and how to use it.

Author Rank :
Page Views : 114428
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

The Windows Forms LinkLabel control is used to display hyperlinks. It is useful when you need to add a control in your application that allows users to visit your home page or any other Web sites. In this article, I will discuss some functionality related to the LinkLabel control and how to use it.

It seems like using a LinkLabel control is pretty simple but it could be tricky. When I first used this control, I thought the control should have a link property where I would add the URL of my Web site and it will work. But no. It doesn't work that way.

There are more than once class is involved in using a LinkLabel control. These classes are - LinkLabel.Link, LinkLabel.LinkCollection, LinkLabelLinkedClickEventArgs, and LinkLabelLinkedClickEventHandler.

Actually, a LinkLabel control can have a collection of hyperlinks and this collection is represented by LinkLabel.LinkCollection. Each hyperlink in this collection is represented by the LinkLabel.Link class. Using this class members, you can control each hyperlink of the control individual.

This class provides 5 properties as described in Table 1. 

Property Description

Enabled

Gets or sets a value indicating whether the link is enabled.

Length

Gets or sets the number of characters in the link text.

LinkData

Gets or sets the data associated with the link.

Start

Gets or sets the starting location of the link within the text of the LinkLabel.

Visited

Gets or sets a value indicating whether the user has visited the link.

 
When you click a hyperlink within the control, the LinkClicked event is raised, and the LinkLabel.Link object representing the hyperlink that was clicked is passed as part of the LinkLabelLinkClickedEventArgs object that is passed as a parameter to the event handler. You can use this object to obtain the LinkLabel.Link object associated with the hyperlink that was clicked by the user. The signature of the LinkLabel click event handler looks like the following:

private void linkLabel1_LinkClicked(object sender,System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
}

You can specify the appearance of hyperlinks by using the LinkLabel class properties. These properties are described in Table 2.

Property Description

DisabledLinkColor

Gets or sets the color used when displaying a disabled link.

LinkArea

Gets or sets the range in the text to treat as a link.

LinkBehavior

Gets or sets a value that represents the behavior of a link.

LinkColor

Gets or sets the color used when displaying a normal link.

Links

Gets the collection of links contained within the LinkLabel.

LinkVisited

Gets or sets a value indicating whether a link should be displayed as though it were visited.

VisitedLinkColor

Gets or sets the color used when displaying a link that that has been previously visited.


The LinkLabel.LinkCollection represents the collection of hyperlinks available in the control. The Add, Remove, and Clear method of this class are used to add a new hyperlink, remove an existing hyperlink, and clear all hyperlinks respectively.

So how LinkLabel control opens the browser. Actually LinkLabel doesn't do any thing for you. You need to call the browser when the LinkLabel button is clicked. You write the following code on the LinkLabel button click event handler, which calls the browser automatically when you pass a URL in the Process.Start method:

System.Diagnostics.Process.Start(e.Link.LinkData.ToString());

MSDN documentation suggests the following way to add and open a hyperlink:

// Create a new LinkLabel control.
private LinkLabel linkLabel1 = new LinkLabel();
public void InitializeMyLinkLabel()
{
// Set the control to autosize based on the text content.
linkLabel1.AutoSize = true;
// Position and size the control on the form.
linkLabel1.Location = new System.Drawing.Point(8,16);
linkLabel1.Size =
new System.Drawing.Size(135,13);
// Set the text to display in the label.
linkLabel1.Text = "Click here to get more info.";
// Create a new link using the Add method of the LinkCollection class.
linkLabel1.Links.Add(6,4,www.microsoft.com);
// Create an event handler for the LinkClicked event.
linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler
(
this.linkLabel1_LinkClicked);
// Add the control to the form.
this.Controls.Add(linkLabel1);
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
// Determine which link was clicked within the LinkLabel.
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.
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
}

But I found in most of the cases, you will end up using only one URL. So why bother writing that much code. If you use Visual Studio .NET, here is the simplest way to use the LinkLabel control. You can find the LinkLabel control in Toolbox as you can see from the following figure:

 

After adding a LinkLabel control to the Form, double click on the LinkLabel control that adds a LinkLabel click event handler. Now simply call the Process.Start method with your URL. For example, my code for opening www.kskin.net looks like following: 

private void linkLabel1_LinkClicked(object sender,System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start(www.kskin.net);
}

Note: You can even write this code on a normal Button or any other control click event handler, if you don't want to use the LinkLabel control.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Good Work by daminabo On March 29, 2007
Nice forum. but how do I send my problems in C#.Net programming to the forum for answer.
Reply | Email | Modify 
Re: Good Work by Mahesh On March 29, 2007

Thanks.

Click on Forums link on the header, select a proper forum category and post your question there.

Reply | Email | Modify 
link button by murali On November 5, 2007
give an idea how i can use link button .i,e show a form when i click button.
Reply | Email | Modify 
linklabel by ron On January 22, 2008
System.Diagnostics.Process.Start(www.kskin.net); I am getting error on line above. The name www does not exist in current context Any reason why?
Reply | Email | Modify 
THANK YOU! by samantha On August 20, 2008
You saved me a TON of time with this posting. It worked like a charm! Thanks!
Reply | Email | Modify 
wow nice by danny On May 30, 2009
thanks, thats just what i was looking for!


---------------------------------
Reply | Email | Modify 
to an open an window in C#? by Gokul On February 1, 2011
In C# using an Linklabel how to open an window ?
Reply | Email | Modify 
thnk by serkan On September 22, 2011
thnk
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.