Results 1 to 16 of 16

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

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

    I am finally getting around to upgrading an old program from VB6 to VB2015 and I have run into my first hurdle. It appears that VB2015 doesn't do control arrays. In VB6 if you wanted a collection of 15 checkboxes you could copy and paste at design time and end up with a checkbox control array named check_myname(0), check_myname(1), check_myname(2), etc and it was very easy to make changes to the property values.

    As far as I can tell VB2015 can't do this! This is so frustrating.

    I used this VB2015 code at runtime to create 15 checkboxes on tabpage4 of my tabcontrol1:

    Code:
            Dim offset = 45
            For tmp = 0 To 14
                Dim checkBox_myname = New CheckBox()
                TabPage4.Controls.Add(checkBox_myname)
                checkBox_myname.Location = New Point(390, offset)
                checkBox_myname.Size = New Size(300, 17)
                checkBox_myname.BackColor = Color.FromArgb(82, 92, 106)
                checkBox_myname.ForeColor = Color.Silver
                checkBox_myname.FlatStyle = FlatStyle.Standard
                checkBox_myname.Font = New System.Drawing.Font("Tahoma", 8, FontStyle.Bold)
                checkBox_myname.TextAlign = ContentAlignment.MiddleLeft
                checkBox_myname.BringToFront()
                offset = offset + 21
            Next tmp
    It all works great. The only problem is that now when it comes time to changing the properties of the dynamically created checkboxes. There doesn't seem to be an easy elegant solution like in VB6.

    For instance in VB6 if I wanted to change the visible property of a specific checkbox it would be something like this:

    Code:
    check_myname(5).Visible = false
    Very simple and elegant. How on earth would you do this in VB2015? All the solutions I have seen are insanely complicated. Am I missing an easy solution?

    Thanks.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

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

    The reason that VB6 provided designer support for control arrays is that it provided a mechanism to handle an event for multiple controls with a single method. VB.NET doesn't need that because it can handle events for multiple objects is a different way so there's no designer support for control arrays. In VB.NET, if you want an array of controls then you create it in code, just like you would any other array. You can then access the elements by index, just as you would with any other array.

    I have to wonder though, why are you using code at all to create those CheckBoxes? There's nothing in that code that depends on any run-time input so why wouldn't you simply add all the controls in the designer? If your issue is positioning then you can simply use a TableLayoutPanel or set the Margin for each control and then let the snap lines in the designer do the positioning. If the reason you're using code is that they are on a dynamically created TabPage then I'd suggest that you should be creating a user control, which means you can still create the CheckBoxes in the designer.

  3. #3

    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
    In VB.NET, if you want an array of controls then you create it in code, just like you would any other array. You can then access the elements by index, just as you would with any other array
    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)?

    Quote Originally Posted by jmcilhinney View Post
    I have to wonder though, why are you using code at all to create those CheckBoxes? There's nothing in that code that depends on any run-time input so why wouldn't you simply add all the controls in the designer?
    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.

    Basically I am using the checkboxes to display a list of filenames that the user can select. Yes, I suspect you will say a checkedlistbox or even a regular listbox is the traditional method to do something like this. However I am not a fan of Visual Studio's control limitations. I want to have total control of the layout of the information and the easiest way this is done is with checkboxes.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  5. #5

    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?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

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

    Sigh. Please stop being so vague. Someone like myself who is learning a new version of a language needs concrete examples.

    As mentioned above I can not figure out the syntax of how to change the properties of a checkbox in the array. You seem to be intentionally avoiding giving concrete examples lol.

    Perhaps an array isn't the best way to go with vb2015 (I have no idea and is why I am posting). As mentioned several times above I want the ability to easily change the property of a checkbox using a variable to identify which checkbox I want to modify.

    VB6 code example:
    Code:
    check_myname(5).Visible = false
    Last edited by RickB2006; Feb 20th, 2017 at 01:11 AM.

  8. #8
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

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

    There is a way doing an array simmilar. You probably meant something like this:

    Code:
    Dim chkboxes = New CheckBox() {Checkbox1, Checkbox2, Checkbox3, Checkbox5}
    
      For Each chk In chkboxes
              
         If chk Is Checkbox5 Then
              chk.Checked = True
         End If
    
      Next

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

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

    Thanks for the reply but the problem with that code is that you haven't used a variable in the checkbox name. My communications skills suck and I think this is the reason for the confusion, lol.

    In VB6 I could assign a number to a variable and then use that variable to modify the property of a control. 15 checkbox would be in a control array (check_myname (0), check_myname(1), check_myname(3), etc)

    VB6 code example:
    Code:
    dim myvariable as long
    myvariable = 5
    check_myname(myvariable).Visible = false
    Sorry guys for the confusion. I appreciate the patience.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

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

    Quote Originally Posted by RickB2006 View Post
    Sigh. Please stop being so vague.
    Seriously? Vague? Since when is "exactly the same" vague? If your VB6 code looks like this:
    Code:
    check_myname(5).Visible = false
    and I tell you that the VB.NET code is exactly the same then means that the VB.NET code looks like this:
    Code:
    check_myname(5).Visible = false
    Is that still too vague for you? If your VB6 code looks like this:
    Code:
    dim myvariable as long
    myvariable = 5
    check_myname(myvariable).Visible = false
    then your VB.NET code would look EXACTLY THE SAME, with the one caveat that you would use Integer rather than Long in VB.NET.

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

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

    Umm, everything that I have read online says that VB2015 doesn't use control arrays like VB6. You certainly can't copy and paste to create the arrays like you can in VB6.

    As an example in VB6 you would take your checkbox control named "check_myname" at design time and copy and paste it thus creating an array populated with check_myname(0), check_myname(1), etc.

    I have tried creating a control array like in the code shown in my first post. I could not change the properties of a specific checkbox as you claim. It is not done in the same way as the old VB6 method.

    Okay so here is what I think you are saying:

    - Create all 15 checkboxes at design time and use those names in the code below (for the example we will keep the names you gave):

    Code:
        Private checkBoxes As CheckBox()
         
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            checkBoxes = {CheckBox1, CheckBox2, CheckBox3}
        End Sub
    Then if I want to change the Visible property of a specific checkbox I would use:
    Code:
    checkBoxes(myvariable).Visible = false
    Last edited by RickB2006; Feb 20th, 2017 at 03:12 AM.

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

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

    Okay that worked. Thanks jmcilhinney and LuckyLuke82.

    I have been using VB6 for so long now it is almost painful to learn knew tricks.

    I imagine to you guys it seemed obvious where I was going wrong but I honestly spent 2 evenings researching this and couldn't find the answer. As usual it seems so simple once you know the solution, lol.

    Thanks again!

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

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

    Quote Originally Posted by RickB2006 View Post
    Umm, everything that I have read online says that VB2015 doesn't use control arrays like VB6. You certainly can't copy and paste to create the arrays like you can in VB6.
    Here's what I posted in my very first reply:
    Quote Originally Posted by jmcilhinney View Post
    The reason that VB6 provided designer support for control arrays is that it provided a mechanism to handle an event for multiple controls with a single method. VB.NET doesn't need that because it can handle events for multiple objects is a different way so there's no designer support for control arrays. In VB.NET, if you want an array of controls then you create it in code, just like you would any other array. You can then access the elements by index, just as you would with any other array.
    That's basically all you needed to know.

  14. #14

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    10

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

    Hope you don't make a living as a teacher, lol.

    That may have been crystal clear in your mind. Doesn't mean it is to other people with different knowledge levels.

    edit: that sounded harsher than I meant to. Thanks for assistance!
    Last edited by RickB2006; Feb 20th, 2017 at 06:01 AM.

  15. #15
    Member
    Join Date
    Jan 2015
    Posts
    40

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

    I'd like to join in this conversation (a year later).
    I am also moving from VB6 to VB.net and find the absence of control arrays frustrating. It's all very well creating the controls at run time, but how do you place them on your form? For example, if I want a number of labels of text boxes, how do I place them where I want them on the form?

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

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

    Quote Originally Posted by PaMarn View Post
    I'd like to join in this conversation (a year later).
    I am also moving from VB6 to VB.net and find the absence of control arrays frustrating. It's all very well creating the controls at run time, but how do you place them on your form? For example, if I want a number of labels of text boxes, how do I place them where I want them on the form?
    You create the control, set its properties as required and then add it to the appropriate parent, e.g.

    vb.net Code:
    1. 'Add a Label to the form.
    2. Dim lbl As New Label
    3.  
    4. With lbl
    5.     .Left = 100
    6.     .Top = 10
    7.     .Width = 50
    8.     .Height = 25
    9. End With
    10.  
    11. Me.Controls.Add(lbl)
    12.  
    13. 'Add a TextBox to a Panel.
    14. Dim tb As New TextBox With {.Left = 10, .Top = 10, .Width = 50, .Height = 25}
    15.  
    16. Me.Panel1.Controls.Add(tb)
    That's pretty much exactly what happens when you create controls in the designer too. That simply generates code that gets executed when the form is created. If you want to see that code, click the Show All Files button at the top of the Solution Explorer window and then expand the node for the form.

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