繁体   English   中英

如何将规则添加到textBox textchanged事件?

[英]How can add rules to textBox textchanged event?

我想做的是:

  1. 当用户开始在textBox2中键入任何内容时,请先将其设为空,然后开始显示用户正在键入的内容。

  2. 然后,如果用户删除了他键入的内容,则再次显示原始文本。

现在它的作用是我需要自己删除里面的文本,但是当我尝试键入任何内容时,由于它是空的,所以我再次看到了原始文本。

private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text != "" || textBox2.Text == "Enter Email Account")
            {
                textBox2.Text = "Enter Email Account";
                button2.Enabled = false;
            }
            else
            {
                button2.Enabled = true;
            }
        }

您应该使用水印。 但是,如果您希望在开始时这样做,可以在属性中设置默认值。 首先,您需要在EnterLeave事件期间创建事件处理程序;

private void Form1_Load()
    {
        textBox1.Text = "Enter Email Account";
        textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
        textBox1.LostFocus += new EventHandler(textBox1_Leave);
    }

然后在Enter上 ,应删除水印文本;

protected void textBox1_GotFocus(object sender, EventArgs e)
    {
        if (textBox1.Text == "Enter Email Account")
        { 
            textBox1.Text = "";

        }
    }

然后您重新检查请假事件;

protected  void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            { 
                textBox1.Text = "Enter Email Account";
            }
         }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM