适用于Windows应用程序的互斥CheckBox

时间:2012-07-17 08:16:47

标签: .net vb.net

我一直在尝试编写一个互斥的CheckBox。

Windows窗体中有两个复选框。如果CheckBox1已选中,则CheckBox2应为UnChecked。我一直试图通过处理CheckBoxes的CheckedChanged事件来实现这一点,但已进入无限循环。

我的代码片段如下。

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

有人有解决方案吗?

修改 我不想在这里使用RadioButtons。

5 个答案:

答案 0 :(得分:2)

对所涉及的每个复选框按钮使用相同的事件处理程序,并使用静态局部变量

防止再入
Private Sub chkBox_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkOnlyMyArea.CheckedChanged 
     Static inCheck as Boolean 
     if inCheck = true then 
         Exit Sub
     end if
     inCheck = true
     chkBox1.CheckState = CheckState.UnChecked    
     chkBox2.CheckState = CheckState.UnChecked    
     CheckBox c = CType(sender, CheckBox)
     c.CheckState = CheckState.Checked    
     inCheck = false
End Sub 

答案 1 :(得分:2)

你搞砸了经过检查的if:你的每一个处理程序都在自行解雇,因此无限循环。 这应该有效:

Private Sub chkBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkBox1.CheckedChanged
    If chkBox1.CheckState = CheckState.Checked And chkBox2.CheckState = CheckState.Checked Then
        chkBox2.CheckState = CheckState.Unchecked
    End If
    If chkBox1.CheckState = CheckState.Unchecked And chkBox2.CheckState = CheckState.Unchecked Then
        chkBox2.CheckState = CheckState.Checked
    End If
End Sub


Private Sub chkBox2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkBox2.CheckedChanged
    If chkBox2.CheckState = CheckState.Checked And chkBox1.CheckState = CheckState.Checked Then
        chkBox1.CheckState = CheckState.Unchecked
    End If
    If chkBox2.CheckState = CheckState.Unchecked And chkBox1.CheckState = CheckState.Unchecked Then
        chkBox1.CheckState = CheckState.Checked
    End If   

End Sub

这可以避免间接递归

答案 2 :(得分:0)

我找到的最简单的方法(适用于超过2个复选框)是在设置选中之前测试其他未选中的方法。这会杀死无限循环。我知道这是一个旧帖子,但这个方法演示了3个或更多独家复选框:

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = True Then
        CheckBox2.Checked = False
        CheckBox3.Checked = False
    ElseIf CheckBox2.Checked = False And CheckBox3.Checked = False Then
        CheckBox1.Checked = True
    End If
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    If CheckBox2.Checked = True Then
        CheckBox1.Checked = False
        CheckBox3.Checked = False
    ElseIf CheckBox1.Checked = False And CheckBox3.Checked = False Then
        CheckBox2.Checked = True
    End If
End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    If CheckBox3.Checked = True Then
        CheckBox1.Checked = False
        CheckBox2.Checked = False
    ElseIf CheckBox1.Checked = False And CheckBox2.Checked = False Then
        CheckBox3.Checked = True
    End If
End Sub

答案 3 :(得分:0)

将此checkchanged处理程序添加到组框中的所有复选框。简单。

Private Sub ToggleCheckBoxOnEntry(sender As Object, e As EventArgs)
    Static CurrentlySelectedbox As CheckBox

    If CType(sender, CheckBox).Checked Then
        CurrentlySelectedbox = sender
    End If

    For Each cntrl As CheckBox In grpVentMode.Controls
        If cntrl.Checked AndAlso cntrl.Name <> CurrentlySelectedbox.Name Then
            cntrl.Checked = False
        End If
    Next

End Sub

答案 4 :(得分:0)

当我有两个以上的复选框并且需要它们相互排斥时,但是我知道以后会添加更多复选框,这是我的工作:

 public class ObjectGroup
{
    public string ObjectName;
    public int GroupID;

    public ObjectGroup(int groupID, string objectName)
    {
        GroupID = groupID;
        ObjectName = objectName;
    }
}

在表单定义中:

private List<ObjectGroup> ObjectGroups;

在表单加载中:

//Set up checkbox groups
ObjectGroups = new List<ObjectGroup>();
ObjectGroups.Add(new ObjectGroup(1, "chStatic"));
ObjectGroups.Add(new ObjectGroup(1, "chNonStatic"));

然后在表单正文中:

private void chNonStatic_CheckedChanged(object sender, EventArgs e)
{
    SetCheckboxState(sender as CheckBox);
}

private void chStatic_CheckedChanged(object sender, EventArgs e)
{
    SetCheckboxState(sender as CheckBox);
}

此函数可以驻留在帮助器类中或形式本身中:

private void SetCheckboxState(CheckBox original)
    {
        var thisName = original.Name;
        var isChecked = original.Checked;
        //Find the object collection to which this control belongs
        var thisObject = (from q in ObjectGroups where q.ObjectName == thisName select q).FirstOrDefault();
        if (thisObject == null) return;
        var otherObjects = (from q in ObjectGroups where q.GroupID == thisObject.GroupID && q.ObjectName != thisName select q.ObjectName).ToList();
        foreach (var o in otherObjects)
        {
            CheckBox obj = this.Controls.Find(o, true).FirstOrDefault() as CheckBox;
            if (obj != null)
            {
                if (isChecked && obj.Checked) obj.Checked = false;
            }
        }
    }