views:

210

answers:

3

How can I make a control array? Or the equivalent.

I am used to Visual Basic 6 which presents the option of whether to create a control array when I copy and paste and control.

+3  A: 

Control arrays are long gone. Object-oriented methods are now the law of the land for VB.

You need to use a collection of some sort, such as a list.

Brad
A: 

A control array in VB6 solely existed to allow a single event handler for multiple controls of same type.

You do this in VB.NET by putting the controls into the Handles clause:

private sub Button_Click(...) Handles Command1.Click, Command2.Click, Command3.Click

end sub
GSerg
Not solely. They also allowed you to put more than 256 controls on one form.
Kaniu
@Kaniu Not necessarily. You could use `Controls.Add` to do the same without creating a control array.
GSerg
@GSerg Only if you want (and are able to) add the controls at run time. But if there are for example a dozen tab pages full of controls, that might not really be an option.
Kaniu
+1  A: 

Another implicit feature of the control arrays that is easily overlooked is the association of numeric indexes with each element of the array. The indexes can be any positive int as long as they areunique in the collection; they do not have to be sequential. Consequently the array is more like a dictionary indexed by integers with each item value being an instance of a specific type of control.

Logic in the VB6 event handlers for the control array get the value of the index along with the attributes of the event to be handled. Logic in the handler typically uses the index to determine which instance of the control was raising the event.

.NET event handlers are quite different. You will typically get a reference to a specific control instance and an instance of a specific event object with the attributes of the event to be handled. You will NOT get the index.

Also, VB6 apps sometimes have logic that iterates/manipulates the control array as an array.

In our default translations, we try to support legacy VB6 logic that depends explicitly on the control array and its indexes. We rewrite control arrays as a group of individual control instances and then we add them to a generic OrderDictionary<int, controlType> during form initialization. The individual controls subscribe to the events and we can use the collection to lookup the index given a control instance or to iterate/manipulate the items in the "array". If you do not explicitly need the index or the array, you can get rid of the collection.

Dynamically adding controls is more work now -- it was conceptually like adding an item to the control array and supported with a single statement in VB6 (Load control). As far as I know, in .NET you need to clone a control, deep copy the properties explicitly, and hook up the event handlers explicitly. It can be generalized using reflection and other moderately advanced techniques -- but it sure is not simply calling "load control". If there is an easier way to do this in .NET I would love to hear about it. FWIW: we translate to a helper function.

mark