Quote Originally Posted by RickB2006 View Post
I wanted to create a control array similar to what we did in VB6 and the only way to do that was at runtime. As mentioned above with VB6 we used to simply copy and paste to do that. VB2015 doesn't allow that.
That's no reason to create controls in code. A control is a control. If it exists, you can put it in an array or a collection, no matter how it was created.
Quote Originally Posted by RickB2006 View Post
Can you give a specific example of how to do this? I have tried and can't get the syntax to work. Considering the VB2015 code I gave above how would you do modify the Visible property of a specific checkbox (as we used to in VB6 as shown above)?
The one potential gotcha is that you can't simply do this:
vb.net Code:
  1. Private checkBoxes As CheckBox() = {CheckBox1, CheckBox2, CheckBox3}
That's because that code gets executed before the constructor and the controls you add in the designer aren't created until the constructor is executed. That means that you can't populate the array until after the constructor:
vb.net Code:
  1. Private checkBoxes As CheckBox()
  2.  
  3. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4.     checkBoxes = {CheckBox1, CheckBox2, CheckBox3}
  5. End Sub
You now have an array of CheckBoxes that you can use like any other array. Of course, if you're going to use literal values to do the indexing then the array is pointless anyway, because you can simply refer to the CheckBox directly, i.e. use 'CheckBox1' rather than 'checkBoxes(0)'. Only if you're using variables for the indexing is an array worthwhile to begin with.