PC Review


Reply
Thread Tools Rate Thread

2-dimensional Array in VB.Net

 
 
=?Utf-8?B?U2hheWFhbg==?=
Guest
Posts: n/a
 
      19th Nov 2006
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.
 
Reply With Quote
 
 
 
 
Michael C
Guest
Posts: n/a
 
      20th Nov 2006
"Shayaan" <(E-Mail Removed)> wrote in message
news:1D34AC60-CD7D-438E-BE55-(E-Mail Removed)...
>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


 
Reply With Quote
 
James CC
Guest
Posts: n/a
 
      20th Nov 2006
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
Hope that helps,James"Shayaan" <(E-Mail Removed)> wrote in
message news:1D34AC60-CD7D-438E-BE55-(E-Mail Removed)...
>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.



 
Reply With Quote
 
=?Utf-8?B?U2Vhbg==?=
Guest
Posts: n/a
 
      20th Nov 2006
Michael,
Would you be able to provide me an example of 1D array that will hold the
Text and the Tag properties? Thanks

"Michael C" wrote:

> "Shayaan" <(E-Mail Removed)> wrote in message
> news:1D34AC60-CD7D-438E-BE55-(E-Mail Removed)...
> >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
>
>
>

 
Reply With Quote
 
Michael C
Guest
Posts: n/a
 
      20th Nov 2006
"Sean" <(E-Mail Removed)> wrote in message
news:16787A54-5A89-451C-B971-(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Export 1-dimensional array values to a two-dimensional table? =?Utf-8?B?TGF1cmll?= Microsoft Excel Programming 2 8th Nov 2007 03:51 PM
Extracting single dimensional array out of two dimensional array Mukesh Microsoft C# .NET 5 24th Oct 2007 11:22 PM
flatten multi-dimensional array to on-dimensional array per9000 Microsoft C# .NET 8 4th Dec 2006 09:46 AM
copy 1 dimensional to 2 dimensional array with actual int values j-in-uk Microsoft C# .NET 3 12th May 2006 09:23 AM
RE: array copy from single-dimensional to multi-dimensional =?Utf-8?B?bWFyaw==?= Microsoft VB .NET 0 30th Jul 2004 11:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:32 PM.