VS 2010:如何在从文本框中删除后禁用按钮?

Tom*_*omG 0 c# textbox button winforms

在Visual Studio 2010中,如果文本框中没有任何内容,我希望该按钮被禁用.它在禁用时启动,当我在文本框中输入内容时启用它.但是,当我从文本框中删除所有内容时,它仍然启用.这就是我所做的:

    public Form1()
    {
        InitializeComponent();
        button1.Enabled = false;
    }       

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text == null)
        {
            button1.Enabled = false;
        }
        else
        {
            button1.Enabled = true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

有什么建议?

谢谢!

Eri*_* J. 5

这条线

if (textBox1.Text == null)
Run Code Online (Sandbox Code Playgroud)

应该

if (textBox1.Text == string.Empty)
Run Code Online (Sandbox Code Playgroud)

Text属性不为null(通常用于表示没有任何值),而是string.Empty,表示长度为零的字符串.

写这个的更简单的方法是:

button1.Enabled = (textBox1.Text != string.Empty);
Run Code Online (Sandbox Code Playgroud)