2

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?

2
  • 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. Commented Jun 3, 2013 at 16:33
  • 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. Commented Jun 3, 2013 at 16:36

5 Answers 5

3

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
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

1

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

1 Comment

Even better if they use a panel, groupbox, or similar to create a logical container for the checkboxes to use instead the Me reference.
0

Are you creating the checkboxes in code or in the designer? If in code, add the checkboxes into a List as you create them. If you're using the designer, add all the checkboxes to a List in the constructor.

  Private myCheckboxes As New List(Of CheckBox)

  Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    myCheckboxes.Add(checkbox1)
    myCheckboxes.Add(checkbox2)
    '''etc

  End Sub

Comments

0

Try something like this out

Dim CtrlNm = "MyControlName"
Dim HeresMyControl As Label = CType(Controls.Find(CtrlNm, True)(0), Label)

This makes the most sense to use in my opinion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.