2-dimensional Array in VB.Net

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to loop through a listbox and create a 2-dimensionla array to hold
each items's Text and the Tag.
When the array is created, then I need to retrieve each Text and the Tag
stored in the array.


Thanks in advance.
 
Shayaan said:
I need to loop through a listbox and create a 2-dimensionla array to hold
each items's Text and the Tag.
When the array is created, then I need to retrieve each Text and the Tag
stored in the array.

No, you should create a 1d array and store a class or udt that has a text
and tag property. Or, create a 1D array of ints and store the index to the
item in the listbox.

Michael
 
Dear Shayaan,

I'm sorry, I don't really do VB (!) but there's something that I just used
in C# that might make what you want to do easier. It worked fine.

You can create a class that has the text for an item in the listbox, a value
(something meaningfull for your program), and indeed other variables as
well, all contained in one class. Then you can create an ArrayList of that
class, and run the ListBox directly off that. This way all the data you want
for each ListBox item is stored in (and retreived from) the ListBox
directly.

Advantage is that you don't have to mess about making sure that the listbox
and your array are synchronized, and you can just adjust items in the array
to change the listbox. The disadvantage is that you can't always do what you
would normally do to the ListBox, eg to clear the items, instead of
ListBox.Items.Clear() you do MyArrayList.Clear(). Try it, it'll make better
sense and I found it worked well.

It also works for anything based on ListBox, eg ComboBox.

Check out ListBox.ValueMember, ListBox.DisplayMember and ListBox.DataSource,
as well as ListBox.SelectedValue, ListBox.SelectedItem, ListBox.SelectedText
for more details. An example from MSDN is :

Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections

Public Class USState
Private myShortName As String
Private myLongName As String

Public Sub New(strLongName As String, strShortName As String)
Me.myShortName = strShortName
Me.myLongName = strLongName
End Sub 'New

Public ReadOnly Property ShortName() As String
Get
Return myShortName
End Get
End Property

Public ReadOnly Property LongName() As String
Get
Return myLongName
End Get
End Property

Public Overrides Function ToString() As String
Return Me.ShortName + " - " + Me.LongName
End Function 'ToString
End Class 'USState


Public Class ListBoxSample3
Inherits Form
Private ListBox1 As New ListBox()
Private textBox1 As New TextBox()

<STAThread()> _
Shared Sub Main()
Application.Run(New ListBoxSample3())
End Sub 'Main

Public Sub New()
Me.ClientSize = New Size(292, 181)
Me.Text = "ListBox Sample3"
ListBox1.Location = New Point(24, 16)
ListBox1.Name = "ListBox1"
ListBox1.Size = New Size(232, 130)
textBox1.Location = New Point(24, 160)
textBox1.Name = "textBox1"
textBox1.Size = New Size(240, 24)
Me.Controls.AddRange(New Control() {ListBox1, textBox1})

' Populates the list box using DataSource.
' DisplayMember is used to display just the long name of each state.
Dim USStates As New ArrayList()
USStates.Add(New USState("Alabama", "AL"))
USStates.Add(New USState("Washington", "WA"))
USStates.Add(New USState("West Virginia", "WV"))
USStates.Add(New USState("Wisconsin", "WI"))
USStates.Add(New USState("Wyoming", "WY"))
AddHandler ListBox1.SelectedValueChanged, AddressOf
ListBox1_SelectedValueChanged
ListBox1.DataSource = USStates
ListBox1.DisplayMember = "LongName"
ListBox1.ValueMember = "ShortName"
End Sub 'New

Private Sub InitializeComponent()
End Sub 'InitializeComponent

Private Sub ListBox1_SelectedValueChanged(sender As Object, e As
EventArgs)
If ListBox1.SelectedIndex <> - 1 Then
textBox1.Text = ListBox1.SelectedValue.ToString()
End If
End Sub 'ListBox1_SelectedValueChanged
End Class 'ListBoxSample3
 
Michael,
Would you be able to provide me an example of 1D array that will hold the
Text and the Tag properties? Thanks
 
Sean said:
Michael,
Would you be able to provide me an example of 1D array that will hold the
Text and the Tag properties? Thanks

See james' example.

Michael
 
Back
Top