vb.net VB - 取消选中组框中的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21060917/
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
VB - Unchecking Check Boxes in a Group Box
提问by Marcus Eagle
Currently I have 5 group boxes all filled with checkboxes, when I want to unselect all of them (for a 'clear selection' button), I use this code that I found on a forum:
目前,我有 5 个组框,全部都填充了复选框,当我想取消选择所有复选框时(对于“清除选择”按钮),我使用在论坛上找到的以下代码:
For Each CheckBox In grpbox_Hiragana
CheckBox.checked = "false"
Firstly, I'm sure if this is the correct way to unselect the checkboxes, secondly the "grpbox_Hiragana" groupbox returns the following error:
首先,我确定这是否是取消选中复选框的正确方法,其次“grpbox_Hiragana”组框返回以下错误:
Expression is of type 'System.Windows.Forms.GroupBox', which is not a collection type
表达式属于“System.Windows.Forms.GroupBox”类型,它不是集合类型
If anyone could confirm this is the correct way of doing this/ help fix the error by telling me why the groupbox won't be accepted that would be great.
如果有人可以确认这是这样做的正确方法/通过告诉我为什么不接受 groupbox 来帮助修复错误,那就太好了。
采纳答案by Al-3sli
if you have all check box on one group box use this code :
如果您在一个组框中拥有所有复选框,请使用以下代码:
Dim ChkBox As CheckBox = Nothing
' to unchecked all
For Each xObject As Object In Me.GroupBox1.Controls
If TypeOf xObject Is CheckBox Then
ChkBox = xObject
ChkBox.Checked = False
End If
Next
' to checked all
For Each xObject As Object In Me.GroupBox1.Controls
If TypeOf xObject Is CheckBox Then
ChkBox = xObject
ChkBox.Checked = True
End If
Next
Or you can use CheckedListBoxControl.
或者您可以使用CheckedListBox控件。
回答by OSKM
A alternative with less lines of code is:
较少代码行的替代方法是:
For Each ChkBox As CheckBox In GroupBox1.Controls
ChkBox.Checked = False
Next
Incidentally your code would have worked if you added .controls, the As CheckBoxjust enables the intellisense (and also ensures it is only Checkbox's that are processed) .
顺便说一句,如果您添加了.controls,您的代码会起作用,As CheckBox只需启用智能感知(并确保仅处理复选框)。

