Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / Visual Basic

TextBoxHint

Rate me:
Please Sign up or sign in to vote.
4.50/5 (17 votes)
11 Mar 2009CPOL3 min read 66.8K   4K   58   19
Standard .NET TextBox control with three additional properties that allow the developer to add a 'hint' to the text...sort of like a tooltip but always visible while the control is empty.
mockup of the TextBoxHint in action

Introduction

An application I'm building has numerous Tree controls on a number of forms. Some of the trees produced contain thousands of rows of data. My users requested a search capability that spanned several columns of data in those trees so I was unable to use the built-in search functions.

That left me to write my own search routines which included input from Text Boxes sized and positioned directly above each column the user could search on... unfortunately that also meant no room for a Label to describe what the user could search on so I thought, "Why not put the label contents inside the TextBox?" and that lead me to build the TextBoxHint control.

Background

I didn't want to use the Text property to hold the description - I also wanted the description to 'fade out' like you'll see in some WPF apps.

Immediately I set out to create a User Control that inherits from a TextBox. I figured I could just override the Paint and from there... sounds logical right? WRONG! Turns out, the .NET TextBox isn't really a .NET control at all! It's a wrapper to a very old Win32 control - Searching on how to override the Paint was nothing short of a several hour, hair and tooth pulling experience but in the end I figured out what was needed by reading an article from a very old, VB website called www.vbaccelerator.com

Using the Cursor

Add a reference to the TextBoxHint.dll into .NET ToolBox and use it just like you would a TextBox control.

TextBoxHint Extended Properties


HintDetails.HintTextThis is the text that is displayed inside the Textbox that will fade out once the user enters data.
HintDetails.HintColorThe color used to paint the HintText
HintDetails.HintFadeSpeedAn integer from 0-5 (if you enter a value < 0, it will default to 0. Enter one > 5 and it will default to 5.
HintDetails.HintTextAlignCan be set to Left (default), Center or Right
CharFilterCan be set to All (default), NumericOnly (0-9) and AlphaOnly (A-Z, a-z)
CharFilterSpecialAllows you to extend the allowed characters selected in CharFilter
If you select CharFilter=NumericOnly and set CharFilterSpecial to "AaBbCc" the TextBox will allow 0-9 and Aa, Bb or Cc.

Using the Control

You can adjust how long the Hint takes to fade out by increasing the HintFadeSpeed:

  • 0 = Instantly vanish when the user enters data into the TextBox
  • 1 = Slow Fade out (approximately 2.5 seconds)
  • 2-9 = Faster Fade out
  • 10 = Fastest Fade out

If you would like to allow for longer or shorter fading, you can adjust the Interval on the Timer used that handles the repainting during fading:

VB.NET
Public Sub New()
	' This call is required by the Windows Form Designer.
	InitializeComponent()

	' Add any initialization after the InitializeComponent() call.
	FadeTimer = New System.Windows.Forms.Timer
	FadeTimer.Interval = 50	' Make this smaller for faster fading 
				' - larger for slower
	FadeTimer.Enabled = False
	AddHandler FadeTimer.Tick, AddressOf FadeTimer_Tick
End Sub

If you would like to extend the range of Fade, adjust the maximum allowed HintFadeSpeed:

VB.NET
Private _HintFadeSpeed As Integer = 3
Public Property HintFadeSpeed() As Integer
	Get
		Return _HintFadeSpeed
	End Get
	Set(ByVal value As Integer)
		_HintFadeSpeed = value
		If value < 0 Then _HintFadeSpeed = 0
		If value > 10 Then _HintFadeSpeed = 10
	End Set
End Property

Sure, I know my use of these rather basic Properties is extremely cryptic. If you would like to see some samples of how you can manage these properties in a more elegant manner, there are numerous, fantastic samples here on CodeProject. (I demonstrate a few in my article, Border Container.)

Points of Interest

This control has the four 'Hint' properties nested inside an expandable Property called HintDetails. You can see how this is done by looking at the Property Converter code found in TextBoxHintDetails.vb.

History

  • 3/5/2009
    • Control is introduced to CodeProject
  • 3/11/2009 - Control is upgraded
    • Added HintTextAlign allowing the developer to align where the HintText is displayed (thanks to Frank for the suggestion)
    • The four Hint properties have been moved into an Expandable Property HintDetails and two additional properties allow the developer to control the type of allowable keys: CharFilter and CharFilterSpecial

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugStrange Appearance Pin
chrisf86578-Mar-15 8:24
chrisf86578-Mar-15 8:24 
GeneralMy vote of 4 Pin
AJMH26-Apr-11 11:38
AJMH26-Apr-11 11:38 
GeneralMy vote of 3 Pin
sopheak.programming13-Nov-10 15:57
sopheak.programming13-Nov-10 15:57 
GeneralProblem with Khmer unicode Pin
sopheak.programming13-Nov-10 15:56
sopheak.programming13-Nov-10 15:56 
GeneralCue Pin
TobiasP21-Apr-09 9:15
TobiasP21-Apr-09 9:15 
GeneralRe: Cue [modified] Pin
Elkay29-Apr-09 10:01
Elkay29-Apr-09 10:01 
General[Message Deleted] Pin
NormDroid21-Mar-09 8:03
professionalNormDroid21-Mar-09 8:03 
GeneralRe: My vote of 1 Pin
Ryleigh27-Mar-09 7:35
Ryleigh27-Mar-09 7:35 
I've read most of YOUR articles, 'Norm' and from the poorly written code to the badly articulated articles, you really have no ground on which to be sending flames out to others as often as you do.

There is something extremely bitter about who and what you are. Try this for help: 14-16 sleeping pills and a bottle of whiskey. The world will not notice.
GeneralRe: My vote of 1 [modified] Pin
NormDroid20-May-09 4:29
professionalNormDroid20-May-09 4:29 
General[Message Deleted] Pin
NormDroid21-Mar-09 8:02
professionalNormDroid21-Mar-09 8:02 
GeneralRe: Crap Pin
Elkay22-Mar-09 13:16
Elkay22-Mar-09 13:16 
GeneralRe: Crap Pin
NormDroid22-Mar-09 21:35
professionalNormDroid22-Mar-09 21:35 
GeneralNice work. Pin
Stumproot17-Mar-09 12:37
Stumproot17-Mar-09 12:37 
GeneralRe: Nice work. Pin
Elkay18-Mar-09 6:52
Elkay18-Mar-09 6:52 
GeneralOverriding WM_PAINT Pin
Matthew Noonan15-Mar-09 15:28
Matthew Noonan15-Mar-09 15:28 
GeneralRe: Overriding WM_PAINT Pin
Elkay15-Mar-09 18:14
Elkay15-Mar-09 18:14 
GeneralHint Test Alignment Pin
fperugini10-Mar-09 1:45
fperugini10-Mar-09 1:45 
GeneralRe: Hint Test Alignment Pin
Elkay11-Mar-09 8:17
Elkay11-Mar-09 8:17 
GeneralRe: Hint Test Alignment Pin
fperugini11-Mar-09 10:47
fperugini11-Mar-09 10:47 

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.