0

I have 6 checkboxes on a form with all of them have the same name except a number at the end. Example chkBr1, chkBr2, chkBr3 etc... ChkBr6. Based on the number of applicants I have I need to set the visible property to true and set the text to the applicant name. My thought to use the index of the for loop to determine this.

bcnt equeals the number of applicants. (number checkboxes out 6 that are visible.

For i as integer = 1 to bcnt
   chkbr[i].visbile = true
   chkbr[1].text = applicant 1
next 

Error looping / setting control name.

1
  • As a hint to a beginner, bcnt doesn't represent "the number of applicants" as a variable name. It represents a cryptic set of four letters. However, Dim numberOfApplicants As.... does clearly indicate intent of the variable. Don't be afraid to use long variable names when they are concise. Embrace them.
    – HardCode
    Commented Aug 28 at 23:31

3 Answers 3

2

You seem to be saying, without actually saying, that you want to show as many CheckBoxes as you have applicants and hide the rest. In that case, you can't just set Visible to True for every control. You need to have the names of the applicants in a list to begin with, then hide or show the CheckBoxes based on that list, e.g.

Dim applicantNames As String() 'Populate as appropriate.
Dim checkBoxes = Controls.OfType(Of CheckBox)().OrderBy(Function(cb) cb.Name).ToArray()
Dim applicantCount = applicantNames.Length

For i = 0 To checkBoxes.GetUpperBound(0)
    Dim cb = checkBoxes(i)

    If i < applicantCount Then
        cb.Show()
        cb.Text = applicantNames(i)
    Else
        cb.Hide()
    End If
Next

This assumes that the CheckBoxes have ben added directly to the form and that they are the only CheckBoxes that have been added directly to the form. If that's not true, you would have to make some slight adjustments.

0

Try this:

Dim i As integer = 1
For Each cb As CheckBox In Controls.OfType(Of CheckBox)()
    cb.Visible = True
    cbText = "Applicant " & i.ToString()
    i+=1
Next

If you might have other checkboxes on the form, put these all in a common container (like a Panel control) and use that as the base.

You may also want to add a .OrderBy() to ensure you process the boxes in order by their name.

1
  • bcnt equals the number of applicants (not the CheckBoxes) -> For i as integer = 1 to bcnt (1 is wrong)
    – Jimi
    Commented Aug 28 at 22:54
0

Here is how I do it to maintain strong-typing.

Dim chkBrs = { chkBr1, chkBr2, chkBr3, chkBr4, chkBr5, chkBr6 }
For i As Integer = 0 To chkBrs.Length - 1
    chkBrs(i).Visible = True
    chkBrs(i).Text = "Applicant " & i + 1
Next

You can find controls by name, but this is the .Name property and not the name of your variable in your code. If you end up with controls that have different names to the variable then that code wouldn't work. The approach I use will continue to work.

1
  • bcnt equals the number of applicants (not the CheckBoxes) -> For i as integer = 1 to bcnt (1 is wrong, both in the OP and here)
    – Jimi
    Commented Aug 28 at 22:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.