-
Apr 21st, 2010, 08:22 PM
#1
Thread Starter
Member
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.
-
Apr 21st, 2010, 08:37 PM
#2
Lively Member
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.
-
Apr 21st, 2010, 09:20 PM
#3
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
-
Apr 21st, 2010, 09:55 PM
#4
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.
-
May 28th, 2010, 02:37 PM
#5
Fanatic Member
Re: Creating An Array Of Combo Box Controls
Originally Posted by jmcilhinney
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.
-
May 28th, 2010, 08:47 PM
#6
Re: Creating An Array Of Combo Box Controls
Originally Posted by IanS
.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.
-
May 29th, 2010, 03:42 AM
#7
Fanatic Member
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 ?
-
May 29th, 2010, 04:20 AM
#8
Re: Creating An Array Of Combo Box Controls
Originally Posted by IanS
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.
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
|