在 c# winform 中更改的文本上启用禁用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14350987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:33:55  来源:igfitidea点击:

Enable disable button on text changed in c# winform

c#winforms

提问by Gourav Goyal

I am developing an application, in which there is a button in search box (like one in itunes). I want to enable cancel button whenever there is text in text box and disable it when text box is empty. I tried with text_changed event on textbox with the following code, but it jump over the if condition. Even sender sends me correct values but i am unable to put it into if else.

我正在开发一个应用程序,其中搜索框中有一个按钮(就像 iTunes 中的一个)。我想在文本框中有文本时启用取消按钮,并在文本框为空时禁用它。我尝试使用以下代码在文本框上使用 text_changed 事件,但它跳过了 if 条件。即使发件人向我发送了正确的值,但我无法将其放入 if else 中。

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(sender.ToString()))
        {
            btn_cancel.Visible = false;
        }
        else
        {
            btn_cancel.Visible = true;
        }
    }

Please help

请帮忙

采纳答案by Inisheer

Here is a simple solution.

这是一个简单的解决方案。

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.button1.Enabled = !string.IsNullOrWhiteSpace(this.textBox1.Text);
    }

Of course, you'll have to set the button.Enabled = false when the form initially loads since the textbox event won't fire on startup (true for all answers currently provided for your question).

当然,您必须在表单最初加载时设置 button.Enabled = false,因为文本框事件不会在启动时触发(对于当前为您的问题提供的所有答案,均为 true)。

回答by sa_ddam213

Try this:

尝试这个:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var textbox = sender as TextBox;
    if (string.IsNullOrEmpty(textbox.Text))
    {
        btn_cancel.Visible = false;
    }
    else
    {
        btn_cancel.Visible = true;
    }
}

sender.ToString()will always return System.Windows.Forms.TextBoxyou need to cast senderas TextBoxand use the Textvalue for your null or empty check

sender.ToString()将始终返回System.Windows.Forms.TextBox您需要转换senderTextBox并使用Text您的空或空支票的值

回答by Afshin

try casting the sender to TextBox :

尝试将发件人强制转换为 TextBox :

if (string.IsNullOrEmpty(((TextBox)sender).Text))

回答by Tommaso Belluzzo

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(textBox1.Text))
        btn_cancel.Visible = false;
    else
        btn_cancel.Visible = true;
}

回答by Simon Whitehead

One liner:

一个班轮:

btn_cancel.Visible = textBox1.Text.Length > 0;

回答by Alex

This is how I'd do it:

这就是我要做的:

private void textBox1_TextChanged(object sender, EventArgs e)
{        
    string text = ((sender as TextBox) == null ? string.Empty : (sender as TextBox).Text);
    this.button1.Enabled = (string.IsNullOrWhiteSpace(text) == false);
}

This doesn't assume the event source is a specific control and avoids an exception if by mistake it's attached to something that's not a TextBox.

这并不假设事件源是一个特定的控件,并且如果错误地将其附加到不是TextBox.