How to use hyperlink functionality in the WinForms TextBoxExt?
Hyperlink support
You can provide hyperlink support to the WinForms Textbox Ext by using the following steps.
1. Implement a custom control that is inherited from the TextBoxExt.
2. Add the LinkLabel control to the TextBoxExt control’s collection.
3. Then categorize and assign the hyperlink text value to the LinkLabel.
4. Start the navigation process upon link selection.
C#
//Sets the text.
this.linkTextBox1.Text = "http://www.google.com";
this.linkTextBox2.Text = "ftp.dlink.ru/pub/";
//Sets the link type.
this.linkTextBox1.LinkType = LinkTypes.Http;
this.linkTextBox2.LinkType = LinkTypes.Ftp;
/// <summary>
/// LinkTextBox extends TextBox with LinkType property that creates a clickable hyperlink when user is not editing the text.
/// </summary>
public class LinkTextBox : TextBoxExt
{
//Initializes the variables.
private LinkLabel llLinkLabel;
//Sets the LinkType.
private LinkTypes ltLinkType = LinkTypes.None;
public LinkTextBox()
{
//Initializes the link label.
llLinkLabel = new LinkLabel();
this.Controls.Add(llLinkLabel);
llLinkLabel.AutoSize = true;
llLinkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(ll_LinkClicked);
llLinkLabel.Text = this.Text;
}
public LinkTypes LinkType
{
get { return this.ltLinkType; }
set
{
this.ltLinkType = value;
if (value == LinkTypes.None)
SwitchToEditMode(true);
else
{
SwitchToEditMode(false);
FillLinkData();
}
}
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
//When control gets focus and active LinkType switches to edit mode.
if (ltLinkType != LinkTypes.None)
this.SwitchToEditMode(true);
}
protected override void OnLostFocus(System.EventArgs e)
{
base.OnLostFocus(e);
//When control gets focus and active LinkType switches to edit mode.
if (ltLinkType != LinkTypes.None)
this.SwitchToEditMode(false);
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged (e);
//When TextBox's Text changes, copies that data to LinkLabel.
if (ltLinkType != LinkTypes.None)
FillLinkData();
}
/// <summary>
/// Switches to edit mode or to clickable mode.
/// </summary>
/// <param name="_bEditMode">Edit mode = true, Clickable mode = false</param>
protected void SwitchToEditMode(bool _bEditMode)
{
//Edit mode means that LinkLabel is not visible.
llLinkLabel.Visible = !_bEditMode;
}
/// <summary>
/// Copies information from TextBox to LinkLabel.
/// </summary>
private void FillLinkData()
{
//Copies the text.
llLinkLabel.Text = this.Text;
//Figures out whether mailto: or http:// link is needed.
string sLinkType = "";
switch (ltLinkType)
{
case LinkTypes.Http:
if (this.Text.ToLower().IndexOf(@"http://") < 0 && this.Text.ToLower().IndexOf(@"https://") < 0)
sLinkType = @"http://";
break;
case LinkTypes.Ftp:
if (this.Text.ToLower().IndexOf(@"ftp://") < 0)
sLinkType = @"ftp://";
break;
case LinkTypes.Email:
if (this.Text.ToLower().IndexOf("mailto:") < 0)
sLinkType = "mailto:";
break;
}
//Clears old links and creates a new one.
llLinkLabel.Links.Clear();
llLinkLabel.Links.Add(0, llLinkLabel.Text.Length, sLinkType + this.Text);
}
/// <summary>
/// Uses the hyperlink when user clicks on a LinkLabel.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (ltLinkType != LinkTypes.None)
{
if (llLinkLabel.Links.Count > 0)
{
string sLink = llLinkLabel.Links[0].LinkData.ToString();
System.Diagnostics.Process.Start(sLink);
}
}
}
}
/// <summary>
/// Types of Hyperlinks that LinkTextBox supports.
/// </summary>
public enum LinkTypes
{
None, // used for regular TextBox
Http, // used for http:// or https:// hyperlink
Ftp, // used for ftp:// hyperlink
Email // used for mailto: hyperlink
}
VB
'Sets the text.
Me.linkTextBox1.Text = "http://www.google.com"
Me.linkTextBox2.Text = "ftp.dlink.ru/pub/"
'Sets the link type.
Me.linkTextBox1.LinkType = LinkTypes.Http
Me.linkTextBox2.LinkType = LinkTypes.Ftp
''' <summary>
''' LinkTextBox extends TextBox with LinkType property that creates a clickable hyperlink when user is not editing the text.
''' </summary>
Public Class LinkTextBox
Inherits TextBoxExt
'Initializes the variables.
Private llLinkLabel As LinkLabel
'Sets the LinkType.
Private ltLinkType As LinkTypes = LinkTypes.None
Public Sub New()
'Initializes the link label.
llLinkLabel = New LinkLabel()
Me.Controls.Add(llLinkLabel)
llLinkLabel.AutoSize = True
AddHandler llLinkLabel.LinkClicked, AddressOf ll_LinkClicked
llLinkLabel.Text = Me.Text
End Sub
Public Property LinkType() As LinkTypes
Get
Return Me.ltLinkType
End Get
Set(ByVal value As LinkTypes)
Me.ltLinkType = value
If value = LinkTypes.None Then
SwitchToEditMode(True)
Else
SwitchToEditMode(False)
FillLinkData()
End If
End Set
End Property
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
MyBase.OnGotFocus(e)
'When control gets focus and active LinkType switches to edit mode.
If ltLinkType <> LinkTypes.None Then
Me.SwitchToEditMode(True)
End If
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
MyBase.OnLostFocus(e)
'When control gets focus and active LinkType switches to clickable mode.
If ltLinkType <> LinkTypes.None Then
Me.SwitchToEditMode(False)
End If
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
MyBase.OnTextChanged(e)
'When TextBox's Text changes, copies that data to LinkLabel.
If ltLinkType <> LinkTypes.None Then
FillLinkData()
End If
End Sub
''' <summary>
''' Switches to edit mode or to clickable mode.
''' </summary>
''' <param name="_bEditMode">Edit mode = true, Clickable mode = false</param>
Protected Sub SwitchToEditMode(ByVal _bEditMode As Boolean)
'Edit mode means that LinkLabel is not visible.
llLinkLabel.Visible = Not _bEditMode
End Sub
''' <summary>
''' Copy information from TextBox to LinkLabel.
''' </summary>
Private Sub FillLinkData()
'Copies the text.
llLinkLabel.Text = Me.Text
'Figures out whether mailto: or http:// link is needed.
Dim sLinkType As String = ""
Select Case ltLinkType
Case LinkTypes.Http
If Me.Text.ToLower().IndexOf("http://") < 0 AndAlso Me.Text.ToLower().IndexOf("https://") < 0 Then
sLinkType = "http://"
End If
Case LinkTypes.Ftp
If Me.Text.ToLower().IndexOf("ftp://") < 0 Then
sLinkType = "ftp://"
End If
Case LinkTypes.Email
If Me.Text.ToLower().IndexOf("mailto:") < 0 Then
sLinkType = "mailto:"
End If
End Select
'Clears old links and creates a new one.
llLinkLabel.Links.Clear()
llLinkLabel.Links.Add(0, llLinkLabel.Text.Length, sLinkType & Me.Text)
End Sub
''' <summary>
''' Use the hyperlink if user clicks on a LinkLabel.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub ll_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs)
If ltLinkType <> LinkTypes.None Then
If llLinkLabel.Links.Count > 0 Then
Dim sLink As String = llLinkLabel.Links(0).LinkData.ToString()
System.Diagnostics.Process.Start(sLink)
End If
End If
End Sub
End Class
''' <summary>
''' Types of hyperlinks that LinkTextBox support.
''' </summary>
Public Enum LinkTypes
None ' used for regular TextBox
Http ' used for http:// or https:// hyperlink
Ftp ' used for ftp:// hyperlink
Email ' used for mailto: hyperlink
End Enum

Figure 1: Link displayed in TextBoxExt
Samples:
Conclusion
I hope you enjoyed learning about how to use hyperlink functionality in the WinForms TextBoxExt.
You can refer to our WinForms TextBoxExt’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms TextBoxExt documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms TextBox and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!