Collections of buttons/textboxes etc.

08-08-2006, 07:48 AM
I'm sure this has been asked before, but couldn't find it in a search.

In VB6, copying and pasting a button or other control gave the option to create a collection. Double-clicking one of the collection's buttons gives:
Private Sub Command1_Click(Index As Integer)
so its easy to tell which button has been pressed.

How do I create a similar collection of buttons etc. in VB2005? Copying and pasting in the designer just gives Button1, Button2 etc. and I can't manipulate those with one chunk of code, I have to copy and paste (with slight differences obviously) a piece of code for each individual button.

08-08-2006, 11:38 AM
This is the link to the forum's tutorial on control arrays
in .net:

There is also a solution that does not use control arrays. You can create one sub to handle multiple events (i.e. different button's being clicked) like this:

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
End Sub

08-09-2006, 02:43 AM
Thanks for that.

Looking at the tutorial, it seems an awful lot of code for what was a really simple operation in VB6 - it's like a step backwards!

As for your code, how do I determine which button was clicked?
'sender' simply returns the object type, not the specific instance, and 'e' tells me that a button was clicked, but not where, or on what.

08-12-2006, 05:38 PM
Errr, sender ought to return the specific instance -- are you sure you're not doing something improperly?

08-13-2006, 02:49 AM
Sender is not an object type its the clicked control boxed as type object. This allows you to write one method for several different controls (amongst other things). To work with this object cast it back to the initial control type. The need to add more code is not an inherant backwards step if it allows for greater flexibility or some other benefits.

08-14-2006, 02:14 AM
Errr, sender ought to return the specific instance -- are you sure you're not doing something improperly?

I don't know if I was doing something wrong, I expected to be able to type:
Index = sender.text
and have it out the text value of the button I clicked into the variable "Index".

I wasn't able to do this, but found some code in the help files that I was able to copy and paste:
Index = DirectCast(sender, Button).Text

This seems to work, although I've no idea how as I don't understand what DirectCast does.

To work with this object cast it back to the initial control type.
It would be helpful if you actually told me how to do that - its fairly obvious I won't know how given I didn't know it had to be done. I'm assuming now that its something to do with the Directcast method I've shown above?

It would also help if I understood why my initial code (above) didn't work?

08-14-2006, 03:13 AM
Your initial code didn't work as sender is of type object and object doesn't have a property called text. When passed in this manner instances of classes (i.e. in this case your button) are boxed to type object so that the method can accept any type of class instance. The DirectCast method unboxes the item allowing you to use it as a button again. If you need to use more than one method/ property of your button you can do..
Dim tmpButton As Button = DirectCast(sender, Button)
tmpButton.Text = "blah"
tmpButton.Enable = false
' etc. etc.Hope thats right, I don't use VB except when trying to help people.

It's very difficult to come up with analogy that makes sense in reality (at least I think so). Heres a poor attempt. Image you are talking about vehicles. You could have a car, an aeroplane, a boat and a submarine. Now all these things are vehicles as such would inherit from the vehicle class. If you had a method (sub or function) which would need to deal with all vehicle types it would accept a parameter of type Vehicle.. eg
Private Sub MoveVehicle(ByRef myVehicle As Vehicle)This flexibilty is helpfull as now you don't have to write lots of methods like...Private Sub MoveCar(ByRef myCar As Car)
Private Sub MovePlane(ByRef myPlane As Plane)
Private Sub MoveBoat(ByRef myBoat As Boat)
'etc. etc.Whilst each vehicle shares many properties/functions that would be inherited from the vehicle class, each vehicle type would likely have their own independent properties. What your code did is the equivalent of calling something like this...
Private Sub MoveVehicle(ByRef myVehicle As Vehicle)
End Sub
Since not all vehicles have wings the vehicle class doesn't have a WingCount property and hence this essentially makes no sense. You need to tell your application that your vehicle is a plane before it can tell you how many wings it has. This is the essence of boxing and unboxing. It's the hiding of all non-inherited properties thus treating the class instance as its base type. I'm sure experts would say this isn't entirely accurate etc, but its hopefully much easier to understand to a begginer than all the technical jargon.

08-14-2006, 03:22 AM
Thanks, that makes perfect sense.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum