Reputation: 351
I have 3 controls that consists of a label, a listbox and a textbox on the same line. On a different line I have 3 different controls of the same type which is label, a listbox and a textbox. I want to put them into a 3 dimensional array like this:
Dim multiArray() As Object = { {Lane1Label, ListBox1, TextBox1},
{Lane2Label, ListBox2, TextBox2} }
But it's not letting me do this. Is there a way?
Upvotes: 1
Views: 491
Reputation: 415600
Make a class (maybe even a custom or user control) to contain each line of controls:
Public Class ControlLine
Public Property Lane As Label
Public Property List As ListBox
Public Property Text As TextBox
End Class
Then create a single dimensional array of these objects (or usually even better: a List(Of ControlLine)
) and put your items in here:
Dim lines() As ControlLine = {
New ControlLine With { Lane = LaneLabel1, List = ListBox1, Text = TextBox1},
New ControlLine With { Lane = LaneLabel2, List = ListBox2, Text = TextBox2}
}
This is much better because the items in your array remain strongly-typed, for good compile-time checking and IDE support for things like intellisense. Recent versions of Visual Studio can also accomplish this via Tuples.
And again, also consider abstracting this further into custom or user controls, where you can create a whole set with one simple constructor call, place one control on the form and have the whole set line up properly, and even think about data binding these ControlLines to a container like a FlowLayoutPanel
instead of managing all the controls and arrays and placement yourself.
Upvotes: 3
Reputation: 117029
Just do this:
Dim multiArray(,) As Object = _
{ _
{Lane1Label, ListBox1, TextBox1}, _
{Lane2Label, ListBox2, TextBox2} _
}
Note the ,
in the declaration of the two-dimensional array.
Upvotes: 0