Click here to Skip to main content
15,867,704 members
Articles / Web Development / HTML
Tip/Trick

LinkLabel with Auto URL Detection

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
14 Oct 2014CPOL 8.9K   3  
A LinkLabel replacement with auto URL detection

Screenshot

Introduction

The standard LinkLabel is a great control to provide a hyperlink to the user. The major problem is the multi-hyperlink usage, then you have to set each link separately with its start position, length and URL. This is quite complicated if you want to show localized text as well.

This replacement allows you just to set the HTML text (including the tags for the related hyperlinks). The given HTML text will be parsed internally and the required links will be set.

Using the Code

Below, you will find the 'extended' LinkLabel class. Its 'Text' property has been set to hidden because it is still used internally by the base class to draw the content.

To set the displayed text, use the property: 'Html' to provide the text including your HTML hyperlink tags (the hyperlinks have to be provided as quoted string) - e.g. 'My text - Click here... - further text'. That's all.

VB.NET
Imports System.ComponentModel

''' <summary>
''' Represents a Windows label control that can display hyperlinks.
''' </summary>
<DesignerCategory("Controls")> _
<Description("Represents a Windows label control that can display hyperlinks.")> _
Public Class LinkLabel
    Inherits System.Windows.Forms.LinkLabel

    Sub New()
        SetStyle(ControlStyles.OptimizedDoubleBuffer Or _
        ControlStyles.ResizeRedraw Or ControlStyles.AllPaintingInWmPaint, True)
    End Sub

#Region " Hidden Properties "
    'property is hidden to set it as 'clean' text (without html tags):
    <Browsable(False), DesignerSerializationVisibility_
    (DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _
    Public Overrides Property Text As String
        Get
            Return MyBase.Text
        End Get
        Set(value As String)
            MyBase.Text = value
            If Not String.IsNullOrEmpty(value) AndAlso value.ToLower.Contains("<a href") Then
                Html = value
            End If
        End Set
    End Property

    <Browsable(False), DesignerSerializationVisibility_
    (DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _
    Public Shadows Property LinkVisited As Boolean
        Get
            Return False
        End Get
        Set(value As Boolean)
            MyBase.LinkVisited = False
        End Set
    End Property

    <Browsable(False), DesignerSerializationVisibility_
    (DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)> _
    Public Shadows Property VisitedLinkColor As Color
        Get
            Return MyBase.VisitedLinkColor
        End Get
        Set(value As Color)
            MyBase.VisitedLinkColor = value
        End Set
    End Property

    <Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> _
    Public Shadows Property LinkArea As LinkArea
        Get
            Return MyBase.LinkArea
        End Get
        Set(value As LinkArea)
            MyBase.LinkArea = value
        End Set
    End Property
#End Region

    Private mHtml As String = String.Empty
    ''' <summary>
    ''' Gets ot sets the text (incl. the html href tag) to be displayed on this control.
    ''' </summary>
    <Description("The text (incl. the html _'<a href=...>', _
    '</a>' tags) to be displayed on this control.")> _
    <Localizable(True)> _
    Property Html As String
        Get
            Return mHtml
        End Get
        Set(value As String)
            If Not mHtml = value Then _setText(value) ' only parse if value has been changed
            mHtml = value
        End Set
    End Property

    ''' <summary>
    ''' Parses the given html text and sets the links.
    ''' </summary>
    Private Sub _setText(text As String)
        Me.Links.Clear()
        Dim s As String = String.Empty
        If Not text.ToLower.Contains("<a href") Then
            ' use whole text as link
            s = text
            Dim area As New LinkArea
            area.Start = 0
            area.Length = s.Length
            MyBase.LinkArea = area
        Else
            ' build link list
            Dim isHref As Boolean = False
            Dim href As String = String.Empty
            Dim isValue As Boolean = False
            Dim value As String = String.Empty
            For i As Integer = 0 To text.Length - 1
                Dim c As Char = text(i)
                If s.ToLower.EndsWith("<a href=") Then
                    ' start href found
                    isHref = True
                    href = String.Empty
                    value = String.Empty
                    ' mid string
                    s = Mid(s, 1, s.Length - 8)
                ElseIf isHref AndAlso c = ">"c Then
                    ' end href found
                    href = Mid(href, 1, href.Length - 1) 'remove quote sign
                    isHref = False
                    isValue = True
                ElseIf isValue AndAlso c = "<"c Then
                    ' end value found
                    s += value
                    Dim l As New LinkLabel.Link
                    l.Start = s.Length - value.Length
                    l.Length = value.Length
                    l.Description = value
                    l.LinkData = href
                    Links.Add(l)
                    i += 3
                    isValue = False
                ElseIf isHref Then
                    ' append link
                    href += c
                ElseIf isValue Then
                    ' append value
                    value += c
                Else
                    ' all other ...
                    s += c
                End If
            Next
        End If

        MyBase.Text = s
    End Sub
End Class

History

  • 12th October, 2014 - Initial version 1.0.0.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Business Analyst
Austria Austria
I am working as Business Analyst and Development Specialist for an international bank since 1993.

Comments and Discussions

 
-- There are no messages in this forum --