2

How do i make it so that when a text box is clicked the text that was originally in the text box ("Enter Text Here") is cleared and only the first time it is clicked?

Edit: Sorry, I'm using C#.

larson
  • 23
  • 5

5 Answers5

5

The most common way to achieve that is to handle the the textbox's focus event (depending on what framework you're using this will vary) and then test for the expected "tip string". If it's there, then you clear the textbox. If not, you leave it alone.

If you only want to show the "tip" once, then you can unsubscribe from the event after you've handled it.

Note that if you give us some more information about what technology you're using (WinForms/WPF/ASP.NET/MVC/jQuery/HTML5/etc.) then a more specific and possibly more robust approach may be possible.

dlev
  • 48,024
  • 5
  • 125
  • 132
  • Using GotFocus event for this is not the best way of achieving this. Consider binding issues you might run into. – michalczerwinski Aug 07 '11 at 18:30
  • @michael fair point, though given that OP hasn't actually stated what technology they're using, I tried to give a general approach to the problem. Nathan, I'm going to roll back your edit. Feel free to post it as a separate answer. – dlev Aug 07 '11 at 18:36
  • @dlev thanks! would upvote if i could. i edited my post to include the technology i am using as you suggested so thanks for that as well. – larson Aug 07 '11 at 19:14
  • @dlev Can you provide an example code? I am having difficulty figuring out how to use this properly. – larson Aug 07 '11 at 20:10
1

Assuming its WinForms App, simply bind a handler for the GotFocus or Click event.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
1

I wouldn't follow the suggestion of changing text in GotFocus event - it will cause problems during binding and is not elegant.

WinForms:

There is special technique to set this kind of tooltip for any standard Windows textbox. Declare this:

private const uint ECM_FIRST = 0x1500;
private const uint EM_SETCUEBANNER = ECM_FIRST + 1;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

and then use:

private void SetWatermark(string watermarkText)
{
    SendMessage(this.Handle, EM_SETCUEBANNER, 0, watermarkText);
}    

HTML:

<input name="email" placeholder="Enter text here">
michalczerwinski
  • 1,069
  • 9
  • 6
0

You can do the following:

  • handle the focus event (Focus) and clear the text if it's the one set as "tip"
  • handle the focus lost event (LostFocus) and if the textbox is empty add the "tip" back to the textbox
virlan2004
  • 243
  • 1
  • 5
0

What you are trying to do is called "Watermarking" a Text-Box. There are a number of methods do to it:

1) use the MouseClick event on the textbox to remove the Default text.
2) use a ready available class to implement it like the one found here: http://vidmar.net/weblog/archive/2008/11/05/watermarked-textbox-in-windows-forms-on-.net.aspx

You can find some more info in similar questions asked on Stackoverflow here are a few:
Watermark / hint text / placeholder TextBox in WPF
Watermark in System.Windows.Forms.TextBox
How to use watermark System.Windows.Forms.TextBox using C#?
Hope this was helpful.
Good Luck.

Community
  • 1
  • 1
pzirkind
  • 2,338
  • 2
  • 20
  • 25