|
|
|
|
Understanding and Using the LinkLabel Control
By
Mahesh Chand October 07, 2002
In this article, I will discuss some functionality related to the LinkLabel control and how to use it.
|
Author Rank:
|
|
Technologies:
Controls,Visual C# .NET |
|
Total downloads : |
|
|
Total page views : |
78326 |
|
Rating : |
|
5/5 |
|
This article has been rated : |
1 times |
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
The Complete Visual C# Programmer's Guide, written by the authors of C# Corner, covers most of the major components that make up C# and the .NETenvironment including Windows Forms, ADO.NET, GDI+, Web Services, and Security. The book is geared toward the beginner to intermediate programmers.
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Mahesh Chand
Mahesh is a software consultant, architect, author, MCP, MVP, and founder of C# Corner. He has over 13 years of experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries including Microsoft, Unisys, Barclay’s, Centocor (J&J), McGraw-Hill, Excelon, PMI, and University of Pennsylvania Hospital. Since year 2000, he is been working with, ASP.NET, SQL Server, C# and .NET. His latest experience and interest is Silverlight, WPF, WCF, XAML and .NET 3.5. If you need any consulting in ASP.NET, AJAX, WPF, WCF, or XAML, contact him at mahesh AT c-sharpcorner DOT com
|
|
|
|
|
|
|
|
|
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.
|
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|