Search code examples
arraysvb.netcheckboxbasic

Assign values to array when a checkbox is checked VB.NET


I have check boxes named checkbox_1, checkbox_2 .... up to checkbox_10. I want to create a loop to store: 1 if the checkbox is checked and 0 if the checkbox is not checked- in the locations(10) array.


Solution

  • That could be done with something like:

    For i As Integer = 1 To 10
        Dim matches() As Control = Me.Controls.Find("checkbox_" & i, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
            Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
            Locations(i - 1) = IIf(cb.Checked, 1, 0)
        End If
    Next