Results 1 to 16 of 16

Thread: [RESOLVED] [vb 2015] Upgrading from VB6 - Checkboxes in control array?

Hybrid View

  1. #1
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [vb 2015] Upgrading from VB6 - Checkboxes in control array?

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

    Re: [vb 2015] Upgrading from VB6 - Checkboxes in control array?

    Quote Originally Posted by jmcilhinney View Post
    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).



    Quote Originally Posted by jmcilhinney View Post
    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.
    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?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [vb 2015] Upgrading from VB6 - Checkboxes in control array?

    Quote Originally Posted by RickB2006 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width