VB - 取消选中组框中的复选框:答案不起作用

时间:2016-10-21 09:42:56

标签: vb.net checkbox groupbox

我找到了关于如何执行此操作的问题的可能答案here

然而,该问题的代码:

Sub UnCheckBoxes()
Dim ChkBox As CheckBox = Nothing
    ' to unchecked all
    For Each ChkBox As Object In Worksheets("Check sheet").Report_Checks.Controls
        If TypeOf xObject Is CheckBox Then
            ChkBox = xObject
            ChkBox.Checked = False
        End If
    Next
End Sub

产生'编译错误:语法错误'。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:3)

根据我的评论。将代码切换为:

Sub UnCheckBoxes()
    Dim ChkBox As CheckBox = Nothing
    ' to unchecked all
    For Each xObject As Object In Worksheets("Check sheet").Report_Checks.Controls
        If TypeOf xObject Is CheckBox Then
            ChkBox = xObject
            ChkBox.Checked = False
        End If
    Next
End Sub

xObject对象用作循环类,以循环遍历Report_Checks中的所有控件。然后,If语句检查xObject是否为CheckBox类型。然后将ChkBox设置为xObject,以便您可以访问它的CheckBox属性并指定.Checked = False

答案 1 :(得分:3)

这是一个解决方案,它使用OfType来消除每次检查对象类型的需要

For Each ChkBox As CheckBox In Worksheets("Check sheet").Report_Checks.Controls.OfType(Of CheckBox)
    ChkBox.Checked = False
Next