An Overview Of Link Label In C#
Introduction:
The LinkLabel control exhibits links to otherobjects, such as files or Web pages. Class LinkLabel is derived fromclass Label and therefore inherits all of class Label?s functionality.A LinkLabel looks as underlined text. When the mouse moves above thelink, the pointer modify to a hand. This is alike to the behavior of ahyperlink in a Web page. The link can change color to indicate whetherthe link is new, visited or active. When clicked, the LinkLabelgenerates a LinkClicked event.
Class LinkLabel :
Class LinkLabel is derived from class Label and therefore inherits all ofclass Label?s functionality. Hand image displays when mouse moves over LinkLabel.
Some common Properties of LinkLabel:
ActiveLinkColor – Denotes the color of the active link when clicked. Default is red.LinkColor – Denotes the original color of every links prior to they have been visited. Default is blue.
Links – Lists the LinkLabel Link objects, which are the links enclosed in the LinkLabel.
LinkVisited – If True, link appears as though it were visited (itscolor is changed to that specified by property VisitedLinkColor).
Text – Denotes the text to show on the control.
VisitedLinkColor – Denotes the color of visited links. Default is purple.
LinkArea – Denotes which part of text in the LinkLabel is treated as element of the link.
LinkBehavior – Denotes the link?s behavior, for example how the link come into view when the mouse is positioned over it.
LinkClicked – Generated when link is clicked.
Now let us see the usage of LinkLabel control with an example:
In this example I place a LinkLabel control with the text "To know C# -visit www.csharpheaven.com – Click Here!" and provide a link to aportion of the text. So that when clicked, the LinkLabel generates aLinkClicked event.
The Example:
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
public class LinkLabel : System.WinForms.Form {
private System.WinForms.LinkLabel linkLabel1;
public LinkLabel() {
InitializeComponent();
}
public override void Dispose() {
base.Dispose();
}
public static void Main(string[] args) {
Application.Run(new LinkLabel());}
private void InitializeComponent()
{
this.linkLabel1 = new System.WinForms.LinkLabel();
linkLabel1.Text = "To know C# – visit www.csharpheaven.com – Click Here!";
linkLabel1.Size = new System.Drawing.Size(168, 56);
linkLabel1.LinkColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)128,(byte)255, (byte)255);
linkLabel1.LinkArea = new System.Drawing.Point(42, 11);
linkLabel1.ForeColor = System.Drawing.Color.Maroon;
linkLabel1.Font = new System.Drawing.Font("Times New Roman", 10f,
System.Drawing.FontStyle.Bold);
linkLabel1.TabIndex = 0;
linkLabel1.TabStop = true;
linkLabel1.Location = new System.Drawing.Point(8, 16);
linkLabel1.LinkClick += new System.EventHandler(linkLabel1_LinkClicked);
this.Controls.Add(linkLabel1);
this.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
}
protected void linkLabel1_LinkClicked(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("IExplore"," http://www.csharphelp.com");
}
}
Here the above program is written by hand (Adding LinkLabelcontrol to a form programmatically)
The program below is the code generated by the WINDES.exe.
// Win32Form1.cs
namespace Win32Form1Namespace {
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
/// <summary>
/// Summary description for Win32Form1.
/// </summary>
public class Win32Form1 : System.WinForms.Form {
/// <summary>
/// Required by the Win Forms designer
/// </summary>
private System.ComponentModel.Container components;
private System.WinForms.LinkLabel linkLabel1;
public Win32Form1() {
// Required for Win Form Designer support
InitializeComponent();
// TODO: Add any constructor code after InitializeComponent call
}
/// <summary>
/// Clean up any resources being used
/// </summary>
public override void Dispose() {
base.Dispose();
components.Dispose();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
public static void Main(string[] args) {
Application.Run(new Win32Form1());
}
/// <summary>
/// Required method for Designer support – do not modify
/// the contents of this method with an editor
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.linkLabel1 = new System.WinForms.LinkLabel();
//@design this.TrayHeight = 0;
//@design this.TrayLargeIcon = false;
//@design this.TrayAutoArrange = true;
linkLabel1.Text = "To know C# – visit www.csharpheaven.com – Click Here!";
linkLabel1.Size = new System.Drawing.Size(184, 56);
linkLabel1.LinkColor = System.Drawing.Color.Teal;
linkLabel1.LinkArea = new System.Drawing.Point(42, 11);
linkLabel1.ForeColor = System.Drawing.Color.Maroon;
linkLabel1.Font =
new System.Drawing.Font("Times New Roman", 10f, System.Drawing.FontStyle.Bold);
linkLabel1.TabIndex = 0;
linkLabel1.DisabledLinkColor = System.Drawing.Color.Yellow;
linkLabel1.TabStop = true;
linkLabel1.Location = new System.Drawing.Point(8, 16);
linkLabel1.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)192, (byte)128);
linkLabel1.LinkClick += new System.EventHandler(linkLabel1_LinkClick);
this.Text = "Win32Form1";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.KeyPreview = true;
this.TransparencyKey = System.Drawing.Color.White;
this.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
this.ClientSize = new System.Drawing.Size(272, 149);
this.Click += new System.EventHandler(Win32Form1_Click);
this.Controls.Add(linkLabel1);
}
protected void Win32Form1_Click(object sender, System.EventArgs e)
{
}
protected void linkLabel1_LinkClick(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("IExplore", "http://www.csharphelp.com" );
linkLabel1.LinkVisited = true;
}
}
}












No comments yet... Be the first to leave a reply!