How do I reference controls with a variable in visual basic 2010?

21,151

Solution 1

Use the Controls.Find() method:

    Dim matches() As Control
    For i As Integer = 1 To 12
        matches = 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)
            If cb.Checked Then

            End If
        End If
    Next

Solution 2

You can access the checkboxes using the Controls collection. However, you need to assign (or convert) from a Control to a Checkbox to access the Checked property, something like this:

Dim ctl As Control
Dim chk As CheckBox

For Each ctl In Controls
  If TypeOf ctl Is CheckBox Then
    chk = ctl
    If chk.Checked Then MsgBox(chk.Name & "is checked")
    End If
  Next ctl

Solution 3

Something like this should work:

    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
        If cb.Name.Contains("checkbox") AndAlso cb.Checked Then
            Select Case cb.Name
                Case "checkbox1"
                    'do stuff
                Case "checkbox2"
                    'do stuff
                Case "checkbox3"
                    'do stuff
                    'and so on ...
            End Select
        End If
    Next

if you change your naming system for the checkboxes to include a delimeter(checkbox-1), and make the other checkbox names uniquely different(chckbox13) you could shorten the code this way:

    For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
        If cb.Name.Contains("checkbox") AndAlso cb.Checked Then
            edit(cell(2, Integer.Parse(cb.Name.Split("-")(1)) + 7))
        End If
    Next
Share:
21,151

Related videos on Youtube

user2448416
Author by

user2448416

Updated on October 21, 2021

Comments

  • user2448416
    user2448416 over 2 years

    I have 12 checkboxes (32 total, but for now just worrying about the first 12) that are named checkbox1, checkbox 2...checkbox 12. I want a for loop to go through them and see if they are checked. If they are checked, it makes changes to an excel sheet, if not it just continues. I have the logic for the spreadsheet edits and the basic structure of the for loop down, but don't know if there is a way to reference the controls using the counter in the for loop.

    For example:

     for i as integer = 1 to 12
       if ("Checkbox" & i).checked = True Then
           <--Spreadsheet things happen-->
       End if
     then
    

    I have had some people suggest a few things, namely using an array with the checkbox names and then doing checkboxes(i).checked but that leads to quite a few issues. Someone else suggested using controls.containskey and CType but while that doesn't give any compile or run time errors, nothing in the spreadsheet is actually changed and I have no idea what any of what I did means.

    Does anyone know a simple way of doing this?

    • Chris Stauffer
      Chris Stauffer almost 11 years
      Your checkboxes are all making different changes based on their value, aren't they? If so then iterating through a collection won't really help since you'd have to have a switch or bunch of if statements anyways.
    • user2448416
      user2448416 almost 11 years
      Yes, this is winforms, and iterating will help (I think). I want checkbox1, if checked to edit cell (2, 8) of a spreadsheet, checkbox2 to edit cell (2, 9) of the same sheet and so on. My for loop checks to see if ("Checkbox" & i) is checked, and if so edits cell (2, i+7). If not checked, it will just continue to the next value for i.
  • Joel Coehoorn
    Joel Coehoorn over 2 years
    Even better if they use a panel, groupbox, or similar to create a logical container for the checkboxes to use instead the Me reference.