Check which checkbox is checked with a loop

What will be the syntax for validation inside the visual base form panel for checkboxes and searches that are being validated? I understand how I can use the for loop and if statement, but I am confused by the syntax for checking each checkbox. eg:

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
+3
source share
3 answers

You need to go through the Controls collection of the control into which the check box is added. Each Control object has a collection of Controls. I would prefer the β€œevery” loop in this scenario, so I will immediately get Control without having to use it using the Controls index. If your CheckBoxes are added to the panel directly, the easiest way to do this would be ...

For Each ctrl As var In panel.Controls
    If TypeOf ctrl Is CheckBox AndAlso DirectCast(ctrl, CheckBox).IsChecked Then
        'Do Something
    End If
Next
+7
source

VB.Net, psudo-:

ForEach CheckBox in ControlContainer
  DoSomething
Next

CheckBox - , a Panel - , a CheckBox.

+2

Try the following:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If CheckBoxList1.Text = "" Then
                do/display something
                Exit Sub
            Else
                For Each item As ListItem In CheckBoxList1.Items
                    If item.Selected Then
                        do/display something
                    End If
                Next
            End If
        End Sub
0
source

All Articles