|
-
Feb 20th, 2017, 12:01 AM
#1
Re: [vb 2015] Upgrading from VB6 - Checkboxes in control array?
 Originally Posted by RickB2006
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.
 Originally Posted by RickB2006
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:
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:
Private checkBoxes As CheckBox() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load checkBoxes = {CheckBox1, CheckBox2, CheckBox3} 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.
-
Feb 20th, 2017, 12:27 AM
#2
Thread Starter
New Member
Re: [vb 2015] Upgrading from VB6 - Checkboxes in control array?
 Originally Posted by jmcilhinney
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.
Ah I am starting to understand what you mean. I still have my head in VB6 methods. I am used to the Control array using the control's name as the control array variable name ie. check_myname(5).
 Originally Posted by jmcilhinney
vb.net Code:
Private checkBoxes As CheckBox() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load checkBoxes = {CheckBox1, CheckBox2, CheckBox3} 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.
Pardon my thick and old brain, lol. I think I am failing to describe properly what I am trying to do.
In the old VB6 method I used a control array to create a column of checkboxes that can be identified and changed by a variable. So if I only wanted to modify a property in the checkbox called "check_myname(5)" I could use the contents of a variable to identify that specific checkbox ie check_myname(myvariable). How would I do something like this in VB2015?
-
Feb 20th, 2017, 12:53 AM
#3
Re: [vb 2015] Upgrading from VB6 - Checkboxes in control array?
 Originally Posted by RickB2006
So if I only wanted to modify a property in the checkbox called "check_myname(5)" I could use the contents of a variable to identify that specific checkbox ie check_myname(myvariable). How would I do something like this in VB2015?
In exactly the same way. An array is an array. You access an element by index.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|