vb.net 禁用复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18020822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:31:07  来源:igfitidea点击:

Disabling CheckBoxes

vb.netcheckbox

提问by bbesase

If I have 4 different CheckBoxes and when the user selects one of them I want the other 3 to become disabled so you can't click on a checkbox while another one is already checked how would I go about doing this? I have this, but it doesnt work right now and I thought it would:

如果我有 4 个不同的复选框,并且当用户选择其中一个时,我希望其他 3 个被禁用,因此您无法单击复选框,而另一个复选框已被选中,我将如何执行此操作?我有这个,但它现在不起作用,我认为它会:

    If NoDelayCheckMarkBox.Checked = True Then
        timeBetweenIterationDelay = 0
        SecondDelayCheckMarkBox.Enabled = False
        HalfSecondDelayCheckMarkBox.Enabled = False
        FiftyMSDelayCheckMarkBox.Enabled = False

I can still click as many check boxes as I want too. Thank you for any help.

我仍然可以根据需要单击任意数量的复选框。感谢您的任何帮助。

回答by KyleMit

As @brian already said, Radio buttons seem like a more organic way to achieve this result, but you still could do this with checkboxes if you wanted

正如@brian 已经说过的那样,单选按钮似乎是实现此结果的更有机的方式,但如果您愿意,您仍然可以使用复选框来做到这一点

Handle the CheckBox.CheckedChangedevent with the same sub for all four checkboxes

CheckBox.CheckedChanged为所有四个复选框使用相同的子处理事件

Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) _
    Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
    'cast sender
    Dim senderCheck As CheckBox = DirectCast(sender, CheckBox)

    'loop through all checkboxes
    For Each checkbox In {CheckBox1, CheckBox2, CheckBox3, CheckBox4}

        'only apply changes to non-sender  boxes
        If checkbox IsNot senderCheck Then

            'set property to opposite of sender so you can renable when unchecked
            checkbox.Enabled = Not senderCheck.Checked
        End If
    Next
End Sub