jeudi 20 janvier 2022

C# checkbox behavior

I have two checkboxes, and I am trying to make one disable the other based on a certain condition.

private void chkBox1_CheckedChanged(object sender, EventArgs e)
{
            if (!chkBox1.Checked && chkBox2.Checked)
            {
                chkBox2.Checked = false;
            }
}

That condition being that chkBox1 is unchecked and chkBox 2 is checked, as per the if statement. What ends up happening is that chkBox1.Checked value also flips from false to true inside of the if despite there being nothing inside of the if to change it. I have debugged and looked at the designer code to see if anything in there is causing issues, but I don't see anything. In designer, there is a wire-up statement of

this.chkBox1.CheckedChanged += new System.EventHandler(this.chkBox1_CheckedChanged);

which is obviously needed to get into the method to check the condition and I want chkBox1's checkChanged event to go into the condition. I'm new to C#, but not new to debugging, however this has stumped me for several days, any suggestions?




Aucun commentaire:

Enregistrer un commentaire