简体   繁体   中英

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. What I need is to enabled the button if the textBox value changed. 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. The button will only enabled if I changed the value for example to 4 or any number but not 3.

Cache the original value somewhere then compare in the TextChanged Event

    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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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