New to Telerik UI for WinFormsStart a free 30-day trial

How to create RadLinkLabel

Updated over 6 months ago

Environment

Product VersionProductAuthor
2020.1.113RadLabel for WinFormsNadya Karaivanova

Description

A common requirement is to use label to display a hyperlink similar to MS LinkLabel control. Although, Telerik UI for Winforms does not support currently such control out of the box you can create your own RadLinkLabel control that can display links and allow users to click their way from page to page.

This tutorial demonstrates how you can create such link label with RadLabel control.

link-label.gif

Solution

First, you should create a custom RadLinkLabel control that inherits from RadLabel class. Then, override the OnMouseEnter method in order to change the mouse arrow into little hand and change the text color. You should override the OnMouseLeave method as well in a way to return back to the default cursor arrow and use another text color if you want to indicate somehow that the link has already been visited.

A full code snippet is illustrated below:

C#
public class RadLinkLabel : RadLabel
{
    bool visited;

    public RadLinkLabel()
    {
        this.Font = new Font("Segoe UI", 12, FontStyle.Regular);
        this.Text = "Telerik";
        this.ForeColor = Color.Blue;
        visited = false;
    }

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        Process.Start("www.telerik.com");
        visited = true;
        this.ForeColor = Color.FromArgb(128, 96, 204);
    }

    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        this.Font = new Font("Segoe UI", 12, FontStyle.Underline);
        this.ForeColor = Color.Black;
        this.Cursor = Cursors.Hand;
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        this.Font = new Font("Segoe UI", 12, FontStyle.Regular);
        if (visited)
        {
            this.ForeColor = Color.FromArgb(128, 96, 204);
        }
        else
        {
            this.ForeColor = Color.Blue;
        }
        this.Cursor = Cursors.Default;
    }
}
   
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support