Peter17
Peter17

Reputation: 3092

TextBox for Live Search in Windows Forms

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?

Upvotes: 1

Views: 4092

Answers (2)

321X
321X

Reputation: 3185

See this thread at MSDN for a possible solution: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/93a67793-6426-4d4f-be9d-a9b79725efc8

Upvotes: 1

Jan Christian Selke
Jan Christian Selke

Reputation: 370

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;
    }

Upvotes: 5

Related Questions