When I select one of them I want to check if the other was already selected and if so I want to turnoff the checkmark
It seems unobvious whether you want to turn off the checkmark from the checkbox you've just checked or from the another one. Since I don't have enough reputation yet to ask for clarification in comments, I'll provide both solutions.
If you want to uncheck another checkbox (which make more sense to me), this code should do the trick:
private void active_checkbox_CheckedChanged(object sender, EventArgs e)
{
if (active_checkbox.Checked)
inactive_checkbox.Checked = false;
}
private void inactive_checkbox_CheckedChanged(object sender, EventArgs e)
{
if (inactive_checkbox.Checked)
active_checkbox.Checked = false;
}
And if you want to uncheck the checkbox you've just checked...
private void active_checkbox_CheckedChanged(object sender, EventArgs e)
{
if (inactive_checkbox.Checked)
active_checkbox.Checked=false;
}
private void inactive_checkbox_CheckedChanged(object sender, EventArgs e)
{
if (active_checkbox.Checked)
inactive_checkbox.Checked=false;
}
This should do the trick. But may I suggest simply disabling another checkbox while another one is checked? This should help to avoid confusing the user:
private void active_checkbox_CheckedChanged(object sender, EventArgs e)
{
inactive_checkbox.Enabled=!active_checkbox.Checked;
}
private void inactive_checkbox_CheckedChanged(object sender, EventArgs e)
{
active_checkbox.Enabled=!inactive_checkbox.Checked;
}
All three solutions are simple, scalable, do not create any additional global variables and are able to use the advantage of checkboxes over radiobuttons - ability to leave them both unchecked.
Hope that helps!
RadioButton
s instead ofCheckBox
es. This will ensure that only one option is selected at a time. – dub stylee Nov 20 '15 at 0:38