Resolved Can this be turned into a loop?

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
Say i have 100 checkboxes and i want one string to save all their status. Can this be turned into a loop?

VB.NET:
        Dim startDate As String = ""
        If cb1.Checked Then
            startDate += "1"
        Else
            startDate += "0"
        End If
        If cb2.Checked Then
            startDate += "1"
        Else
            startDate += "0"
        End If
        If cb3.Checked Then
            startDate += "1"
        Else
            startDate += "0"
        End If
        If cb4.Checked Then
            startDate += "1"
        Else
            startDate += "0"
        End If
        If cb5.Checked Then
            startDate += "1"
        Else
            startDate += "0"
        End If
.
.
.
.
        If cb100.Checked Then
            startDate += "1"
        Else
            startDate += "0"
        End If
 
Last edited:
But i dont want to type out 100 controls. Is there and expression where you can combine "cb" and "1" to make the control name where i can do like.

VB.NET:
for i as integer = 1 to 100
   If "cb"+i .checked then

   else

   endif
next

If i do a for each on all the controls, does it always keep the same order? Is the order determine by the order in the designer.vb? The order seems to be when their were added in the designer
I guess i could do the for each control in .controls, but can you combine strings to make a control name?
 
Last edited:
Hi,

Use this snippet to get what you need:-

VB.NET:
For I As Integer = 1 To 100
  Dim myCheckBox As CheckBox = DirectCast(Me.Controls("cb" & I), CheckBox)
  If myCheckBox.Checked Then
    'do what you need here
  End If
Next

Hope that helps.

Cheers,

Ian
 
Back
Top