检查循环选中的复选框

Wil*_*son 2 vb.net forms checkbox for-loop

在visual basic form面板中检查复选框并找到哪些被检查的语法是什么?我理解我如何使用for循环和if语句,但我对于检查每个复选框的语法感到困惑.例如:

Dim i As Integer
For i = 1 To 10
    'Here is where my code would go. 
    'I could have ten checkboxes named in sequence (cb1, cb2, etc), 
    'but how could I put i inside the name to test each checkbox?
Next
Run Code Online (Sandbox Code Playgroud)

phi*_*xed 8

您需要遍历控件的Controls集合,该集合中添加了Checkbox.每个Control对象都有一个Controls集合.在这种情况下,我更喜欢For Each循环,因此我立即获得Control,而不必使用Controls索引.如果您的CheckBox直接添加到Panel,最简单的方法是...

For Each ctrl As var In panel.Controls
    If TypeOf ctrl Is CheckBox AndAlso DirectCast(ctrl, CheckBox).IsChecked Then
        'Do Something
    End If
Next
Run Code Online (Sandbox Code Playgroud)