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
  • 3,052
  • 9
  • 47
  • 77

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,153
  • 2
  • 30
  • 42