views:

159

answers:

6

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.

A: 

You can draw string "Type here" to the textbox background until it empty

Nagg
Interesting idea, I'll try
mafutrct
A: 

Handle the lost focus event and if the property Text is empty, fill it with your default string.

Maurizio Reginelli
+4  A: 

What you're looking for is a texbox with "watermark"

There's a sample implementation for C# here.

Hope it helps

Marcos Placona
A: 

If this is for ASP.NET then you can try TextBoxWatermark.

If this is for Windows Forms, this is already answered here in SO.

ydobonmai
A: 

Why using OnTextChanged? I would suggest to remove the text "Type here" when the TextBox gets focus. When the control loses focus and no text is entered, you can display the text again.

Same result and no need for tricky logic.

lboshuizen
+1  A: 

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):

var $textbox = $("textbox selector"); // assumes you select a single text box
if ($textbox.val() == "") {
   $textbox.val("Type here to...");
   $textbox.one('focus', function() {
     $(this).attr('value', '');
   });
}

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).

macca1