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.
Imports System.ComponentModel
<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 "
<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
<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) mHtml = value
End Set
End Property
Private Sub _setText(text As String)
Me.Links.Clear()
Dim s As String = String.Empty
If Not text.ToLower.Contains("<a href") Then
s = text
Dim area As New LinkArea
area.Start = 0
area.Length = s.Length
MyBase.LinkArea = area
Else
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
isHref = True
href = String.Empty
value = String.Empty
s = Mid(s, 1, s.Length - 8)
ElseIf isHref AndAlso c = ">"c Then
href = Mid(href, 1, href.Length - 1) isHref = False
isValue = True
ElseIf isValue AndAlso c = "<"c Then
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
href += c
ElseIf isValue Then
value += c
Else
s += c
End If
Next
End If
MyBase.Text = s
End Sub
End Class
History
- 12th October, 2014 - Initial version 1.0.0.0