Im trying to write a program in VB.net for a shopping system. It will read through the database and populate the items on the form. The app displays information such as product name etc in labels, inside a scrollable panel. Im creating the objects and assigning values such as text at runtime. I'm using a loop for the code.
If i was using vb 6, i would have a control array, and use the index and my counter to display the data. Since im doing this in vb.net, i have no way to do that.. Any solutions?
There are no Control arrays in VB.NET . But you could iterate through Panel.Controls collection. All the controls are in that collection (If they are all in the same panel).
VB.NET doesn't have control arrays as such.
However, you can create an array of controls and assign your controls to each element of the array, though you could also use a
List(Of Control)
.This will allow you to loop over the collection.
VB.NET does not support control arrays, in the same sense as VB6. You can do similar things, though. For instance, if you want to handle events from multiple controls with the same method, you can do so like this:
If you want to create an array of controls that you can loop through, you can do that to, like this:
Alternatively, if you name your controls consistently, you can find them by name in your form's
Controls
collection. For instance, if you had three text boxes namedTextBox1
,TextBox2
, andTextBox3
, you could loop through them like this:First is there a reason why you can't use a grid for this? - that would be the obvious solution (as it would have been in VB6 as well).
ETA . . .but if you must, this code snippet will add a set of labels to your form. You will need to modify this eg replace the for next loop with a for each r as mydataset.mytabledatarow in mydataset.mydatable etc etc