用于Windows窗体中的Live Search的TextBox

Pet*_*r17 1 .net c# user-interface textbox winforms

如何创建一个文本框,当它为空时显示灰色的"搜索"和用户开始在其中键入文本时的标准行为?

小智 5

通过TextBox事件进入离开以及属性来执行此操作:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记更换ForeColor. (4认同)