Karlx Swanovski
Karlx Swanovski

Reputation: 2989

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.

Upvotes: 0

Views: 1760

Answers (1)

Anthony Queen
Anthony Queen

Reputation: 2358

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;
    }

Upvotes: 4

Related Questions