TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

LinkLabel Example: Windows Forms

This C# article describes the LinkLabel control and its usage for creating visual hyperlinks.

LinkLabel. Hyperlinks are sometimes useful in Windows Forms programs.

The LinkLabel control provides hyperlinks similar to those on web pages. This control has some complexities—it must be initialized in C# code.

Start. Please add a new LinkLabel control to your form by double-clicking or dragging from the Toolbox in Visual Studio. Next, you can right-click on the LinkLabel and adjust some of its properties.

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.

Example that uses LinkLabel: C#

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 = "";
	    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);
	}
    }
}

The Form1_Load method is executed when the program begins. At this stage, we create a new Link element. Link elements are stored in the Links collection on a LinkLabel instance. We call the Add method to store the LinkLabel.

And: The LinkData points to a string reference containing the location of an excellent website.

The LinkData reference is of an object type—you can store anything there and use it through casting. We simply store a string in the location and then, in LinkClicked, we access the Link.LinkData from the parameter "e" and cast it.

Then: We use the Process.Start method to send the URL to the system's default web browser. Process-start.

Text. The Text property on the LinkLabel determines what text will be shown when the LinkLabel is displayed on a Form. 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 and visited link colors in Windows Forms. Also, you can set the disabled color. 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 is useful if you want to have a LinkLabel with two clickable links. To do this, add two 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 is used internally in your program or 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.

Unfortunately: The LinkLabel is not as clear to use as plain HTML would be, but it can be used for more tasks in Windows Forms itself.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf