How to create a Textbox which displays "search" in grey color when it is empty and standard behavior when user starts typing text into it?
Asked
Active
Viewed 3,985 times
1
-
check this link: http://csharpdotnetfreak.blogspot.com/2009/01/winforms-autocomplete-textbox-using-c.html – Waqas Sep 13 '11 at 08:55
-
1Try to look at http://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms/4902969 – default locale Sep 13 '11 at 08:55
-
DveExpress WinForm editors (TextEdit) have this functionality built-in already. Can be set through a property called NullText. – Youp Bernoulli Sep 13 '11 at 10:03
2 Answers
5
Do it via TextBox Events Enter and Leave and Attributes:
private void textBox1_Leave(object sender, EventArgs e)
{
if(textBox1.Text.Trim().Length == 0)
{
textBox1.Text = "Search";
textBox1.ForeColor = Color.LightGray;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}

Jan Christian Selke
- 370
- 3
- 10
-
4
-
By the way, this one will clear the textbox every time I reenter on it. It's too hardcore, isn't it? – default locale Sep 13 '11 at 08:59
-
@MAKKAM: sharp! I edited the code to prevent user input to be resetted every time the users (re)enters the textbox... – Youp Bernoulli Sep 13 '11 at 10:01
1
See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8

321X
- 3,153
- 2
- 30
- 42