只需单击一次即可删除文本框默认文本,c#表单

时间:2016-08-11 17:32:08

标签: c# forms text textbox box

我希望我的Textbox只删除一次我的文字,这样每次点击都不会清除Textbox。我目前的代码如下:

private void textBox1_Click(object sender, EventArgs e)
{
    textBox1.Text = string.Empty;          
}

但是我怎么能让它只删除一次文本?

1 个答案:

答案 0 :(得分:4)

你可以使用一个简单的布尔标志:

public partial class Form1 : Form
{
    bool firstClick = true;

在您的事件处理程序中:

private void textBox1_Click(object sender, EventArgs e)
{
    if (firstClick)
    {
        textBox1.Text = string.Empty;          
        firstClick = false;
    }
}