Click here to Skip to main content
Licence CPOL
First Posted 7 Sep 2009
Views 8,684
Downloads 103
Bookmarked 18 times

Linked TextBox

By Paw Jershauge | 7 Sep 2009
How to make the textbox act as a hyperlink when the Control key is pressed.

1

2
1 vote, 16.7%
3
1 vote, 16.7%
4
4 votes, 66.7%
5
4.69/5 - 6 votes
μ 4.69, σa 1.46 [?]

LinkedTextBox_new.jpg

Introduction

This is a short demo on how to make a textbox act as a hyperlink when the CTRL key is pressed and the control has the focus.

Background

I needed a LinkedTextBox control for a customer.

Using the code

The control has three additional properties:

  • CltrHyperLink
  • HyperLinkColor
  • HyperLinkVisitedColor

A new Event:

  • HyperLinkClicked

And a new Virtual method:

  • OnHyperLinkClicked

Setting the CtrlHyperLink property to true activates all the other properties and events.

HyperLinkColor is the color of the hyperlink when it is in the default state. HyperLinkVisitedColor is the color of the hyperlink in its visited state. Listening to the HyperLinkClicked event will result in the event being fired when the user holds down the Control key and clicks the left mouse button. OnHyperLinkClicked can be overridden if needed.

Overridding some of the features of the TextBox

protected override void OnKeyUp(KeyEventArgs e)
{
    base.OnKeyUp(e);
    FormatTextBox(false); //Set the decoration of the textbox to standard
}

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);
    FormatTextBox(e.Control);
    //if true Set the decoration of the textbox 
    //to an hyperlink else standard.
}

protected override void OnMouseClick(MouseEventArgs e)
{
    base.OnMouseClick(e);
    if (_IsHyperLink && e.Button == MouseButtons.Left)
    //If the the control is in hyperlink mode
    //and the left mouse button was clicked...
    {
        OnHyperLinkClicked(new EventArgs());
        FormatTextBox(false);
    }
}

private void FormatTextBox(bool ShowAsLink)
{
    if (!_CltrHyperLink)
    return;
    if (ShowAsLink)
    {
        Font = new Font(Font, FontStyle.Underline);
        ForeColor = (!_HyperLinkVisited ? _HyperLinkColor : 
                      _HyperLinkVisitedColor);
        Cursor = Cursors.Hand;
        _IsHyperLink = true;
    }
    else
    {
        Font = new Font(Font, FontStyle.Regular);
        ForeColor = SystemColors.WindowText;
        Cursor = Cursors.Default;
        _IsHyberLink = false;
    }
}

protected virtual void OnHyperLinkClicked(EventArgs e)
{
    _HyperLinkVisited = true;
    if (HyperLinkClicked != null)
        HyperLinkClicked(this, e);
}

History

  • 7 Sep. 2009 - First release.

License

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

About the Author

Paw Jershauge

Software Developer

Denmark Denmark

Member
ListView Group Sorter
An Code Generator, for making SQL table into .Net Classses (2nd place in the Code Generation 2008 Competition)
A Class for getting the Rss feed list of a website
Seagate Date Code Calculator
DNSBL lookup Class
Base N converter (N = 10-62)
Lightweight Directory Access Protocol Uniform resource identifier (LDAPUri)
LoginHours from DirectoryEntry into boolean array

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General[Message Deleted] PinmemberDavid Sleeckx2:12 7 Sep '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120210.1 | Last Updated 7 Sep 2009
Article Copyright 2009 by Paw Jershauge
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid