Click here to Skip to main content
14,032,407 members
Click here to Skip to main content
Add your own
alternative version


65 bookmarked
Posted 26 Dec 2004

LinkTextBox - a quick solution for entering emails and hyperlinks

, 11 Apr 2005
Rate this:
Please Sign up or sign in to vote.
A guide to creating a cool looking LinkTextBox control that handles email and other hyperlinks just like MS Outlook.

Sample Image - LinkTextBox.png


Do you want to impress your boss and create professional looking hyperlink entry forms for $0 in only 10 minutes? Who wouldn't? With regular .NET TextBox and HyperLink controls combined, that is really easy. Just follow a few simple steps in this article and you'll have your own full featured LinkTextBox control.


Remember how Email and Web Page textbox in Microsoft Outlook work as a hyperlink when they are not focused, and as a regular textbox when they have focus? If you don't, just open your Outlook and then come back to this article...

Using the code

Let's figure out how it works first. The control is actually a combination of TextBox and LinkLabel controls. HyperLink control is positioned so that it overlaps the TextBox's text and intercepts the clicks. But if a user clicks in the TextBox and not on the LinkLabel, the TextBox gets the focus and LinkLabel hides. LinkTextBox acts and looks exactly as a regular TextBox until it loses focus. Then the LinkLabel is made visible again and the user can click on the link.

What you want to do is extend the regular TextBox. You can do that in your existing application, in any of your libraries, or create a new project and add a Class Object file and extend the System.Windows.Forms.TextBox. You could also use the Add Inherited Control... wizard, but I usually don't. I can type inheritance declaration faster than click through the wizard. In the constructor, let's create an instance of LinkLabel and position it so that it exactly overlaps the TextBox's text. We also need to show it and copy the TextBox.Text to the LinkLabel so it looks OK at design time. We also need to wire up a Clicked event in here.

namespace DavidVidmar.Windows.Forms {

    public class LinkTextBox : TextBox {

        private LinkLabel llLinkLabel;

        public LinkTextBox() {

            llLinkLabel = new LinkLabel();


            llLinkLabel.AutoSize = true;
            llLinkLabel.Left = -1;
            llLinkLabel.Top = 1;
            llLinkLabel.Visible = true;
            llLinkLabel.Text = this.Text;

            llLinkLabel.LinkClicked += 
              new LinkLabelLinkClickedEventHandler(ll_LinkClicked);


Next, let's prepare an enum for various types of links. We need one that tells the user the link feature is disabled, one for email links, one for FTP links and one for web links or secure web links.

public enum LinkTypes {
    None,     // act as a regulator TextBox
    Http,     // act as a http:// or https:// hyperlink
    Ftp,      // act as a ftp:// hyperlink
    Email     // act as a mailto: hyperlink

Now, we can add a LinkType property to LinkTextBox. It tells what kind of link the Text property represents and if it is a link at all. By default, the LinkTextBox will work as a regular TextBox and we need to hide a LinkLabel. We'll call this mode edit mode and we'll use a SwitchToEditMode() method that we will create later. But if it is a real LinkTextBox, we need to switch to clickable mode, where LinkLabel is visible, again with SwitchToEditMode() just with a different parameter value. We also need to copy the information from TextBox to LinkLabel. Let's assume for a moment FillLinkData() method does just that.

private LinkTypes ltLinkType = LinkTypes.None;

public LinkTypes LinkType {
    set { 
        this.ltLinkType = value; 
        if (value == LinkTypes.None) {
        } else {
    get { return this.ltLinkType; }

The SwitchToEditMode() method only needs to show or hide the LinkLabel. We can easily do that in just one line of code:

protected void SwitchToEditMode(bool _bEditMode) {
    llLinkLabel.Visible = !_bEditMode;

The FillLinkData() method we used in the property setter is a little more complicated. We need to copy the value of Text property from TextBox to LinkLabel, try to figure out what protocol to use in our link, and update the Links array of LinkLabel. The easiest way to do that is to create a new entry each time around.

private void FillLinkData() {

    llLinkLabel.Text = this.Text;

    string sLinkType = "";
    switch (ltLinkType) {
        case LinkTypes.Http:            
            if (this.Text.ToLower().IndexOf(@"http://") < 0 && 
                this.Text.ToLower().IndexOf(@"https://") < 0) {            
                sLinkType = @"http://";
        case LinkTypes.Ftp:
            if (this.Text.ToLower().IndexOf(@"ftp://") < 0) {
                sLinkType = @"ftp://";
        case LinkTypes.Email:
            if (this.Text.ToLower().IndexOf("mailto:") < 0) {
                sLinkType = "mailto:";

    llLinkLabel.Links.Add(0, llLinkLabel.Text.Length, sLinkType + this.Text);


We could figure out when exactly to copy the text from TextBox to LinkLabel, but the easy way out is every time the Text property on TextBox changes. It's not optimal, but I don't think anybody will notice that.

protected override void OnTextChanged(EventArgs e) {
    base.OnTextChanged (e);
    if (ltLinkType != LinkTypes.None) {

Now, let's cover the switching from editable to clickable mode. If TextBox control gets the focus, user must have clicked to the right of the LinkLabel in TextBox, so we need to switch to edit mode. If control loses focus, we need to switch to clickable mode. We already created SwitchToEditMode() method that will do everything. All we need to do is override the OnGetFocus() and OnLostFocus() methods.

protected override void OnGotFocus(EventArgs e) {
    if (ltLinkType != LinkTypes.None) this.SwitchToEditMode(true);

protected override void OnLostFocus(System.EventArgs e) {
    if (ltLinkType != LinkTypes.None) this.SwitchToEditMode(false);

It's not very obvious but later on you will realize that some strange focus related issue will surface. If you give focus to a control through tab it will be different than if you shift-tab to the control. With some tricky focus handling, the issue is resolved.

private void llLinkLabel_GotFocus(object sender, EventArgs e) {
  // if control got focus with tab and not because user clicked a link
  // then transfer focus to TextBox and clear the flag
  if (!bLinkClicked) {
    bLinkClicked = false;

private void llLinkLabel_MouseDown(object sender, MouseEventArgs e) {
  // remember that user clicked on the label,
  // so we can correct the focus of a label
  bLinkClicked = true;

Now, let's make this thing clickable! In the constructor, we already wired the event, now let's code it. We need to check if we should use a link at all and then call a UserHyperlink() method that will actually open the link.

private void ll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
    if (ltLinkType != LinkTypes.None) UseHyperlink();

Now, let's figure out how to open a link. Don't even start thinking about directly interacting with IE, Outlook, or any other browser or mail application. Let's use the OS to figure out what to do with the link. This way, we just execute the link, and Windows will open either a default mail application or default browser. This is the right way to do it. Just create a new Process class, call Start() method with the link, and keep your fingers crossed. The bad part is that you don't really know if it will work. Just catch an exception and throw a new one. In an application, just catch this exception, show a MessageBox, and give the user a chance to figure out what's wrong with the link.

private void UseHyperlink() {
    try {
        if (llLinkLabel.Links.Count > 0) {
            string sLink = llLinkLabel.Links[0].LinkData.ToString();
    } catch (Exception ex) {
        throw new ArgumentException("Link error!", ex);

We can easily build in one more feature. If the control is in the edit mode (LinkLabel is hidden), we can help the user test the link. If the user clicks in the control while the Ctrl key is pressed, he can launch the link.

protected override void OnMouseDown(MouseEventArgs e) {
    if (ltLinkType != LinkTypes.None) {
        if (e.Button == MouseButtons.Left && 
           (Control.ModifierKeys & Keys.Control) == Keys.Control) {
        } else {
    } else {

And that's it. The control is ready for use. Just set the LinkType property and the control is ready for some serious clickin'! Or you could even let the user decide if the text in the LinkTextBox is a link with a ComboBox next to it!

I hope you realized how you can create quite powerful controls that could be easily a part of an expensive UI library, with a simple combination of two basic controls and some simple UI processing.

So, before you start Googlin' or even throwing couple of bucks out the window for a commercial control, think about writing your own control. It's the best in terms of control, and when it's this simple, it's really fun.


  • v1.0 - Initial release.
  • v1.1 - Added support for https:// and ftp:// links, issues related to different ways of giving focus to the control is resolved.

Keep checking this page out to see if anyone has found a bug or got an idea of how to extend the LinkTextBox.

You can also come over to my blog and see if there is anything new cooking.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

David Vidmar
Web Developer
Slovenia Slovenia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionWhat if I want to capture the click event? Pin
nybelle21-Dec-11 6:32
membernybelle21-Dec-11 6:32 
GeneralVery useful control Pin
madcat10-Jul-09 3:49
membermadcat10-Jul-09 3:49 
GeneralThanks Pin
Sergio Torres C.13-Feb-06 1:30
memberSergio Torres C.13-Feb-06 1:30 
Generalproblems with Email Link Pin
Rodney Rodríguez16-Jan-06 8:10
memberRodney Rodríguez16-Jan-06 8:10 
GeneralRe: problems with Email Link Pin
David Vidmar16-Jan-06 10:53
memberDavid Vidmar16-Jan-06 10:53 
GeneralAnother minor bug related to ReadOnly Pin
pgolds28-Sep-05 12:53
memberpgolds28-Sep-05 12:53 
GeneralRe: Another minor bug related to ReadOnly Pin
Craig G Fraser30-Sep-05 4:15
memberCraig G Fraser30-Sep-05 4:15 
GeneralRe: Another minor bug related to ReadOnly Pin
pgolds3-Oct-05 6:24
memberpgolds3-Oct-05 6:24 
GeneralNice Control. I converted it to VB.NET. Pin
pgolds28-Sep-05 9:27
memberpgolds28-Sep-05 9:27 
GeneralRe: Nice Control. I converted it to VB.NET. Pin
pgolds28-Sep-05 11:58
memberpgolds28-Sep-05 11:58 
pgolds wrote:
Thing I noticed: The linklabel sometimes receives focus when you're tabbing through the controls on a form. Seems to me that only the textbox should receive focus when you tab into the control. Any thoughts on how to remedy this?

I may have found a solution. Instead of using the GotFocus event of the LinkLabel, I use the 'Enter' event of the LinkLabel. In addition, you need to set the bLinkClicked to False whenever you go into the llLinkLabel_GotFocus (or in my case, the llLinkLabel_Enter) subroutine. In your code, you're only setting the bLinkClicked to False if it's already false. This, of course, fails to ever reset the bLinkClicked. Let me know if you'd like to see the changes. It is in VB.NET, though.

GeneralNice Article Pin
shivonkar27-Apr-05 21:14
membershivonkar27-Apr-05 21:14 
GeneralDesign time support Pin
nagarsoft19-Jan-05 11:03
membernagarsoft19-Jan-05 11:03 
GeneralRe: Design time support Pin
David Vidmar19-Jan-05 20:45
memberDavid Vidmar19-Jan-05 20:45 
GeneralAn alternative way Pin
mav.northwind27-Dec-04 20:55
membermav.northwind27-Dec-04 20:55 
GeneralRe: An alternative way Pin
MWKKit27-Dec-04 22:39
memberMWKKit27-Dec-04 22:39 
GeneralRe: An alternative way Pin
mav.northwind27-Dec-04 23:01
membermav.northwind27-Dec-04 23:01 
GeneralRe: An alternative way Pin
David Vidmar27-Dec-04 22:40
memberDavid Vidmar27-Dec-04 22:40 
GeneralRe: An alternative way Pin
mav.northwind27-Dec-04 23:09
membermav.northwind27-Dec-04 23:09 
GeneralRe: An alternative way Pin
David Vidmar28-Dec-04 0:14
memberDavid Vidmar28-Dec-04 0:14 
Generalhttps Pin
gabis27-Dec-04 0:07
membergabis27-Dec-04 0:07 
GeneralRe: https Pin
David Vidmar27-Dec-04 0:14
memberDavid Vidmar27-Dec-04 0:14 
GeneralRe: https Pin
Anonymous13-Apr-05 12:58
memberAnonymous13-Apr-05 12:58 
GeneralRe: https Pin
David Vidmar17-Apr-05 21:28
memberDavid Vidmar17-Apr-05 21:28 
GeneralRe: https Pin
David Vidmar17-Apr-05 21:28
memberDavid Vidmar17-Apr-05 21:28 
QuestionUnexpected behavior? Pin
Carl Mercier26-Dec-04 14:45
memberCarl Mercier26-Dec-04 14:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 11 Apr 2005
Article Copyright 2004 by David Vidmar
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid