Reputation: 39
I have several groupboxes on a windows form with a check box, number picker & combo box. If the check box is not selected I want the other elements disabled. I've disabled everything but the checkboxes on load and then coded 'if/else' criteria on the CheckedChanged event and all works fine. However a couple of group boxes are dependent on another condition from an earlier form. So if a user tries to select that checkbox but the condition isn't valid, I want to display a message, uncheck the checkbox and disable the other elements in the groupbox until the user goes back and changes the condition ... or just accepts things as they are and moves on. All works pretty much as expected, except the message box is displayed twice, i.e. displayed, cancel out, displayed again ... I'm thinking once when the box is checked and again when it's unchecked (if I just leave it checked, but disabled, the message is only shown once, but that's kind of ugly. ;-( Is there a better way to do this?
private void checkbox1_CheckedChanged(object sender, EventArgs e)
{
if(var1 == "YES")
{
if (checkbox1.Checked)
{
numbox.Enabled = true;
combobox1.Enabled = true;
}
else
{
numbox.Enabled = false;
combobox1.Enabled = false;
}
}
else
{
MessageBox.Show("Required condition is not YES");
checkbox1.Checked = false;
}
}
Upvotes: 2
Views: 2793
Reputation: 4766
It doesn't seem like it is necessary to display the message and uncheck the box if the box is already unchecked...so why not just check whether or not the box is checked before you display the message instead of constantly fiddling with the event registration?
private void checkbox1_CheckedChanged(object sender, EventArgs e)
{
if(var1 == "YES")
{
if (checkbox1.Checked)
{
numbox.Enabled = true;
combobox1.Enabled = true;
}
else
{
numbox.Enabled = false;
combobox1.Enabled = false;
}
}
else if (checkbox1.Checked)
{
MessageBox.Show("Required condition is not YES");
checkbox1.Checked = false;
}
}
Upvotes: 1
Reputation: 1893
Instead of the checkbox1_CheckedChanged
event you could use the checkbox1_Click
event to detect the user input. If you change the checkbox1.Checked
state in your programm, the checkbox1_Click
event won't be raised.
private void checkbox1_Click(object sender, EventArgs e)
{
//your code
}
Upvotes: 1
Reputation: 2801
You can remove and readd the event handler as in the below example.
else
{
MessageBox.Show("Required condition is not YES");
checkbox1.CheckedChanged -= checkbox1_CheckedChanged;
checkbox1.Checked = false;
checkbox1.CheckedChanged += checkbox1_CheckedChanged;
}
Upvotes: 1
Reputation: 11399
Like @LarsTech said ... you are calling your event twice. Here is a possible workaround. Just remove your event at the beginning of your methode. In the end ... add it again.
private void checkbox1_CheckedChanged(object sender, EventArgs e)
{
checkbox1.CheckChanged -= checkbox1_CheckedChanged;
if(var1 == "YES")
{
if (checkbox1.Checked)
{
numbox.Enabled = true;
combobox1.Enabled = true;
}
else
{
numbox.Enabled = false;
combobox1.Enabled = false;
}
}
else
{
MessageBox.Show("Required condition is not YES");
checkbox1.Checked = false;
}
checkbox1.CheckChanged += checkbox1_CheckedChanged;
}
Upvotes: 2