简体   繁体   English

如何启用基于textBox禁用的按钮? C#

[英]How to enable a button that is disabled based on textBox? c#

I got a textBox that load value from my database and a button that update changes based on the value of the textBox. 我有一个从数据库中加载值的textBox,还有一个根据textBox的值更新更改的按钮。 What I need is to enabled the button if the textBox value changed. 我需要的是在textBox值更改的情况下启用按钮。 For example, the value that the textBox loads is 3 if I also input again 3 in the textBox the button will still be disable. 例如,如果我也再次在textBox中输入3,则textBox加载的值为3,该按钮仍将被禁用。 The button will only enabled if I changed the value for example to 4 or any number but not 3. 仅当我将值更改为4或任何数字但未更改为3时,该按钮才会启用。

Cache the original value somewhere then compare in the TextChanged Event 将原始值缓存到某个地方,然后在TextChanged事件中进行比较

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text == OriginalValue)
        {
            button1.Enabled = false;
        }
        else 
        {
            button1.Enabled = true;
        }
    }

Alternatively, you could just do this (see CodesInChaos' comment below): 或者,您可以执行此操作(请参见下面的CodesInChaos评论):

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

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

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