Creating An Array Of Combo Box Controls-VBForums
Results 1 to 8 of 8

Thread: Creating An Array Of Combo Box Controls

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    35

    Creating An Array Of Combo Box Controls

    I have a form I am designing for work, and there are 3 combo boxes I want on it that display the same choice (unit of measurement selection). Now instead of 3 seperately named boxes, I wanted to make an array of combo box, now I thought if I created 1, then copy pasted it would make them an array as the same name but it just lists the new one with a default name. Now I want to do this at design time rather than run time and I ahve searched all around and cant see what I need to do.

  2. #2
    Lively Member
    Join Date
    Apr 2010
    Location
    Australia
    Posts
    71

    Re: Creating An Array Of Combo Box Controls

    Why don't you make your own control that has 3 combo boxes in it... you could expose 3 in 1 type stuff with properties & a control would also be re-usable.

  3. #3
    PowerPoster
    Join Date
    May 2002
    Posts
    25,155

    Re: Creating An Array Of Combo Box Controls

    What you are looking for is what was known as a Control Array in VB6.... it no longer exists in .NET. What you can do is create an array of controls... or an array of ComboBoxes or better still a List (Of ComboBox). But it still needs to be filled at run time.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,814

    Re: Creating An Array Of Combo Box Controls

    The question is, why do you want to do that? What advantage does it provide over just having three independent controls? The reason control arrays existed in VB6 was so that you could handle an event for multiple controls the same way. You'd handle an event of the array and then use the control's Index to work out which control raised the event.

    That is no longer required in VB.NET because one method can handle as many events as you like. In the event handler, you use the 'sender' parameter to determine which object raised the event. The reason for control arrays no longer exists to control arrays no longer exist. VB6 developers have become so used to using them though, that they expect them to be there in VB.NET too. An array of controls in VB.NET is no different to an other array, so they are not treated any differently.

    I don't think you even need an array by the sound of it. Like I said, you don't need an array for anything to do with events so, unless you need to loop through all the controls at some point, you don't need an array.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: Creating An Array Of Combo Box Controls

    Quote Originally Posted by jmcilhinney View Post
    The reason control arrays existed in VB6 was so that you could handle an event for multiple controls the same way. You'd handle an event of the array and then use the control's Index to work out which control raised the event.
    No, Event handlers is not the only reason we like control arrays.

    If I had 10 textboxes in an array and I had a single piece of code writing and reading from them then I'd only need one piece of code because it could switch from one box to the other simply by changing the index. For example code to clear all 10 boxes is as simple as this:-

    Code:
    Private Sub ClearBoxes()
        Dim i as Integer
        For i = 1 to 10
           TextBox(i).Text = ""
        Next i
    End Sub
    The same code just loops round. if I wanted to edit the way the boxes are handled I'd just need to edit in one place.

    Doing that to 10 separate boxes would take 10 separate lines of code and if I wanted to change the code I'd have to be careful that I changed all 10 places.

    .Net allows me to very easily create an array of controls in code and use them exactly the same as I did in vb6 - so why doesn't it allow me to create them at design time.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,814

    Re: Creating An Array Of Combo Box Controls

    Quote Originally Posted by IanS View Post
    .Net allows me to very easily create an array of controls in code and use them exactly the same as I did in vb6 - so why doesn't it allow me to create them at design time.
    For exactly the reason that I've already stated: handling events. In VB6 you needed design-time support for control arrays to allow you to handle the same event for multiple controls with one method. That is the only reason that design-time support was required. Anything else that you could do with a control array could just as easily be done with an array created in code. In VB.NET you don't don't need the design-time support for event handling so you don't need the design-time support at all.

    A lot of people migrating from VB6 seem to have the impression that design-time support for control arrays has been removed from VB.NET. That is not the case. Remember that VB.NET was created from the ground up. Any features in VB.NET that resemble features in VB6 had to be added to VB.NET, with all the work that that entails. Sure, being able to create a control array in the designer in VB6 might have been handy but it wasn't created because it was handy. It was created because it was required to enable handling multiple events with a single method. In VB.NET, design-time support for control arrays is not required for event handling so it is not required at all, so it was not added to VB.NET.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    739

    Re: Creating An Array Of Combo Box Controls

    WYSIWYG is great, isn't it ? Well, I think it is.

    An array of something like timers and other controls that aren't visible at runtime is fine. I agree, create them in code.

    But my form is like a large control panel with a 100 or more visible controls. Even using VB6 and being able to drag, size, position them on the form where I want them takes a long time but I can see what they look like and reposition them till I'm happy with the 'look' of my display before locking them in position.

    Creating all those controls in code and positioning, sizing them, starting the app so I can see what it looks like, stopping it again, repositioning them and re-running etc etc is a nightmare.

    Surely a WYSIWYG editor is where we ought to be. Being able to place things on the form and see what it looks like is the whole purpose of WYSIWYG isn't it ?

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    87,814

    Re: Creating An Array Of Combo Box Controls

    Quote Originally Posted by IanS View Post
    WYSIWYG is great, isn't it ? Well, I think it is.

    An array of something like timers and other controls that aren't visible at runtime is fine. I agree, create them in code.

    But my form is like a large control panel with a 100 or more visible controls. Even using VB6 and being able to drag, size, position them on the form where I want them takes a long time but I can see what they look like and reposition them till I'm happy with the 'look' of my display before locking them in position.

    Creating all those controls in code and positioning, sizing them, starting the app so I can see what it looks like, stopping it again, repositioning them and re-running etc etc is a nightmare.

    Surely a WYSIWYG editor is where we ought to be. Being able to place things on the form and see what it looks like is the whole purpose of WYSIWYG isn't it ?
    Um, yeah, and that's exactly where we are. You can create and configure your controls in the visual designer as much as you like. How can you see an array? You can't, so what does that have to do with WYSIWYG editing? The fact is that it doesn't, so why would they add designer support to VB.NET? The simple fact is that VB6 had a requirement for that support so it was added. VB.NET has no such requirement so it has no such support.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.