Conditional "on" and "off" on a C#

Asked

Viewed 83 times

2

How to put a secondary function on a button or element? For example :

private void Bnt1_Click(object sender, EventArgs e)
{   
}

private void Bnt2_Click(object sender, EventArgs e)
{
    bnt.Enabled = false;
}

In this code, when clicking Bnt2 the first button is disabled, how to do so so that when you click Bnt2 again Bnt1 is active again.

Ps: If something has not become clear warn and I will change.

Ps2: The secondary function refers to the conditional part, "otherwise".

  • 1

    What do you call a secondary function of a button? Would it fire two distinct logics at the same time?

  • 1

    I expressed myself badly Jefferson, I wanted to talk about conditional(if & Else) but I ended up getting confused.

  • 1

    Ah, I get it now. I know that João Silva’s answer is exactly about this, but it would be even better if you put this in the question too, even to serve as a basis if someone else finds your same difficulty

1 answer

4


I didn’t understand the part about putting 2 functions in a bathroom but that should do it.

private void Bnt2_Click(object sender, EventArgs e)
{
    if(bnt.Enabled)
        bnt.Enabled = false;
    else
        bnt.Enabled = true;
}

Or at the suggestion of Jefferson Quesado:

private void Bnt2_Click(object sender, EventArgs e)
{
    //valor = ao inverso do valor atual
    bnt.Enabled = !bnt.Enabled;
}
  • 2

    Only you mixed it up a bit bnt and btn; as there are only two characters, I can not edit

  • 3

    Maybe it’ll be more elegant for bnt.Enabled = !bnt.Enabled? So the new enabled value will always be the opposite of the previous, without having to navigate deeper to know the operation

  • 1

    Yes, I didn’t notice that I had switched bnt and btn, really, I didn’t think of that option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.