0

我一直在尝试编写一个互斥复选框。

Windows 窗体中有两个复选框。如果 CheckBox1 已选中,则 CheckBox2 应未选中。我一直试图通过处理 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。

4

5 回答 5

2

您搞砸了检查更改if的 s:您的每个处理程序都在自行触发,因此是无限循环。这应该有效:

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

这避免了间接递归

于 2012-07-17T08:36:29.127 回答
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 
于 2012-07-17T08:27:08.837 回答
1

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

 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;
            }
        }
    }
于 2018-08-14T11:49:40.310 回答
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
于 2014-12-28T22:18:26.373 回答
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
于 2015-03-13T21:30:49.403 回答