1

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?

Peter17
  • 2,912
  • 8
  • 45
  • 75
  • check this link: http://csharpdotnetfreak.blogspot.com/2009/01/winforms-autocomplete-textbox-using-c.html – Waqas Sep 13 '11 at 08:55
  • 1
    Try 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. – Bernoulli IT Sep 13 '11 at 10:03

2 Answers2

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;
    }
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,024
  • 2
  • 29
  • 40