Home
Search
LinkLabel Example: Windows FormsLearn about the LinkLabel control and use it to create visual links.
C#
This page was last reviewed on Oct 5, 2022.
LinkLabel. The LinkLabel control provides hyperlinks similar to those on web pages. This control has some complexities—it must be initialized in C# code.
Getting started. Please add a new LinkLabel control to your Windows Form. Next, you can right-click on the LinkLabel and adjust some of its properties.
Example code. To implement the functionality in this example, you need to create a Load event, which you can do by double-clicking on the enclosing form.
Then Double-click on the LinkLabel instance to create the LinkClicked event handler.
Info The Form1_Load method is executed when the program begins. At this stage, we create a new Link element.
Tip Link elements are stored in the Links collection on a LinkLabel instance. We call the Add method to store the LinkLabel.
Object The LinkData reference is of an object type—you can store anything there and use it through casting.
LinkClicked We store a string in the location. In LinkClicked, we then access the Link.LinkData from the parameter "e" and cast it.
using System.Diagnostics; using System.Windows.Forms; namespace WindowsFormsApplication13 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, System.EventArgs e) { // Add a link to the LinkLabel. LinkLabel.Link link = new LinkLabel.Link(); link.LinkData = "http://www.dotnetperls.com/"; linkLabel1.Links.Add(link); } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // Send the URL to the operating system. Process.Start(e.Link.LinkData as string); } } }
Text. The Text property on the LinkLabel determines what text will be shown. This is a property that you will want to set every time you add a new LinkLabel.
Tip Using the Links collection, you can create multiple links in the same text.
Text
Link colors. You can adjust the active, default, visited and disabled link colors. The active color is the one shown when the user is actively clicking on a link.
And The visited link color appears once the link has been previously clicked—this is often purple.
Multiple links. As noted, you can have multiple links in the same LinkLabel. This results in 2 clickable links. To do this, add Link objects to the LinkLabel in the Load event.
Tip You should specify the lexical position of the links. We do this by specifying the index of the start and the length in characters.
Then In LinkClicked, you can acquire the LinkData from the clicked link in the same way as in the example shown.
Summary. The LinkLabel can be used to launch a web browser. The Links collection is confusing at first but once the concept of multiple links per LinkLabel is understood, it is clear.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 5, 2022 (edit).
Home
© 2007-2022 sam allen.
see site info on the changelog.