1

当我单击文本框时,我希望默认文本消失。是否有任何其他财产可以用于此目的?

4

3 回答 3

4

以下将默认文本设置为灰色。此外,当您将文本框留空时,再次将默认文本设置为文本框。

private void Form1_Load(object sender, EventArgs e)
    {
        this.textBox2.Enter += new EventHandler(textBox2_Enter); 
        this.textBox2.Leave += new EventHandler(textBox2_Leave);
        textBox2_SetText();
    }

    protected void textBox2_SetText()
    {
        this.textBox2.Text = "Default Text...";
        textBox2.ForeColor = Color.Gray;
    }

    private void textBox2_Enter(object sender, EventArgs e)
    {
        if (textBox2.ForeColor == Color.Black)
            return;
        textBox2.Text = "";
        textBox2.ForeColor = Color.Black;
    }
    private void textBox2_Leave(object sender, EventArgs e)
    {
        if (textBox2.Text.Trim() == "")
            textBox2_SetText();
    }

在此处输入图像描述

于 2014-11-04T08:39:35.373 回答
3

GotFocus向 TextBox 的事件添加一个方法,将 Text 属性更改为 ""

private void Form1_Load(object sender, EventArgs e) {
    this.textBox1.GotFocus += new EventHandler(textBox1_Focus); 
    this.textBox1.Text = "some default text...";
}

protected void textBox1_Focus(Object sender, EventArgs e) {
    textBox1.Text = "";
}
于 2013-01-27T03:36:06.320 回答
2

听起来您想在文本框中添加水印。

请参阅这些文章。

于 2013-01-27T04:17:55.933 回答