I have been trying write a Mutually Exclusive CheckBox.
There are two checkBoxes in a Windows Form. If CheckBox1 is Checked then CheckBox2 should be UnChecked. I have been trying to achive this by handling the CheckedChanged event of the CheckBoxes, but has gone into an infinite loop.
My code snippets below.
Private Sub chkBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkBox1.CheckedChanged
If chkBox2.CheckState = CheckState.Checked Then
chkBox2.CheckState = CheckState.Unchecked
End If
If chkBox1.CheckState = CheckState.Unchecked Then
chkBox1.CheckState = CheckState.Checked
End If
End Sub
Private Sub chkBox2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkBox2.CheckedChanged
If chkBox1.CheckState = CheckState.Checked Then
chkBox1.CheckState = CheckState.Unchecked
End If
If chkBox2.CheckState = CheckState.Unchecked Then
chkBox2.CheckState = CheckState.Checked
End If
End Sub
Does someone have a Solution for this?
EDIT
I am not suppose to use RadioButtons here.
You messed up the checked-changing
ifs: each one of your handlers is firing itself, hence the infinite loop.This should work:
This avoids indirect recursion