Displaying "Type here to ..." until the user enters text into a TextBox
is a well-known usability feature nowadays. How would one implement this feature in C#?
My idea is to override OnTextChanged
, but the logic to handle the changes of text from and to "Type here" is a bit tricky...
Displaying "Type here" on initialization and removing it on first input is easy, but I want to display the message every time the entered text becomes empty.
If this is ASP.NET (as opposed to winforms), you could do this:
If you are using jQuery, add this to your document ready (or however you initialize your page):
You'll need to do some small refactoring if you are selecting more than one text box (put the if statement inside of an each on the element).
You can draw string "Type here" to the textbox background until it empty
I'm just starting to learn C# this semester so I'm not an expert, but this worked for me: (This is using windows forms)
If this is for ASP.NET then you can try TextBoxWatermark.
If this is for Windows Forms, this is already answered here in SO.
Based on @Joel's answer. I fix his class (thanks for the base!)
Something that has worked for me:
Where
bool waterMarkActive
is a class member variable andtextBox
is theTextBox
. This probably should be encapsulated though :) There might be some issues with this approach, but I'm not currently aware of any.I recently discovered that Windows support water marks in text boxes; they are called cue banners (see here). It's very easy to implement:
Where
textBox
is an instance ofTextBox
,0x1501
is the code for the windows messageEM_SETCUEBANNER
, thewParam
may either beTRUE
(non-zero) orFALSE
(zero), andlParam
is the water mark you'd like to display.wParam
indicates when the cue banner should be displayed; if set toTRUE
then the cue banner will be displayed even when the control has focus.