0

how can i use variable for for loop inside object ?

e.g. :

enable all checkboxes with for

    Private Function AllEnabled(ByVal b As Boolean)
    Dim i As Integer
    For i = 0 To 15
        CheckBox(i).enabled = b
    Next
End Function

Sorry For Little Speak , & Thanks in Advance

3
  • What problem are you having with that code? Looks okay to me. Commented Jan 30, 2013 at 2:16
  • checkbox(i) gives me error 'CheckBox' is a type and cannot be used as an expression. Commented Jan 30, 2013 at 2:19
  • So that's your problem. The for loop is fine. You just need an array of checkboxes that you can iterate through. Commented Jan 30, 2013 at 2:27

1 Answer 1

3

The CheckBox(i) syntax is classic VB6. VB6 allowed you easily create control arrays directly in the form designer. VB.NET does not share that same feature. You could manually create your own array of check box controls in the code, but when using the designer, every control must be assigned its own unique name. If you named them all sequentially, such as CheckBox1, CheckBox2, etc., then you could access them in a loop by name via the Form.Controls collection:

For i As Integer = 0 to 15
    Dim c As CheckBox = CType(Me.Controls("CheckBox" & i.ToString), CheckBox)
    c.Enabled = b
Next
Sign up to request clarification or add additional context in comments.

1 Comment

@Zaid: if you need so many checkboxes, consider CheckedListBox Class.

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.