How do I use an arraylist and a structure together?

  • Thread starter Thread starter RBCC
  • Start date Start date
R

RBCC

I have a form with a listbox and two textboxes. In the listbox I have the make and models of automobiles. and as the user clicks on the make of the car in the listbox I would like to output the make and model in the textboxes, how is this done with an arraylist

John
 
There are multiple ways you can get where you want to go, but assuming that
you have to do it with an ArrayList and a Strucutre...

Add each struct into the the arraylist in the order they will appear in the
Listbox. At that point, ArrayList(0) will be equal to the listbox 0
itemname. Then you can just CType the ArrayList value TextBox1.Text
=CType(ArrayList(ListBox1.SelectedIndex), MyStruct).Make

TextBox2.Text = CType(ArrayList(ListBox1.SelectedIndex), MyStruct).Model
assuming Make and Model are both strings.

There's a lot undesirable about this approach, the most glaring of which is
storing Value types in a Reference type which can cause you a lot of
problems if you need to change things and you aren't familiar with Boxing
and dealing with value and reference types together.

A much easier way would be to use a Collection or a DataTable. Bind the
ListBox to the column that corresponds to the Make Model (set it's
DispalyMember to this). Use TextBox1.DataBindings.Add("text", DataSEt,
DataTable.ColumName) and a bindingcontext and then just set the text box's
text column to the respective field in the datatable. You can also bind to
a collection (and anything supporting the IList INterface) so that's
probably a more maintainable way to go.

HTH,

Bill
RBCC said:
I have a form with a listbox and two textboxes. In the listbox I have the
make and models of automobiles. and as the user clicks on the make of the
car in the listbox I would like to output the make and model in the
textboxes, how is this done with an arraylist?
Community Website: http://www.dotnetjunkies.com/newsgroups/
 
something like this

file to read: cars.tx
subaru, outbac
chevy,nov
ford ,mustan

imports system.collectio
imports system.i


structure aut
dim make as strin
dim model as strin
end structur

dim car as aut
dim cararray as new arraylis
dim fv as streamreader = file.opentext("cars.txt")' assume that file is in bin dir
form loa
dim carstring() as strin
d
carstring=fv.readline.split(","c
auto.make=carstring(0
auto.model=carstring(1
cararray.add(auto
icounter=icounter+
loop until fv.peek=-

end clas

Private Sub lstcars_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstNames.SelectedIndexChange
txtmake.text=ctype(arraylist(lstcars.selectedindex),auto).mak
txtmodel.text=ctype(arraylist(lstcars.selectedindex),auto).mode
end su

will this work
Joh
 
Hi,

Auto is a keyword use automobile instead. I would use an class
insead of a structure. If you add the class to the arraylist you can
databind to the arraylist. Here is how I would do it. I assume you have
textbox1, textbox2, and listview1 on a form.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load



Dim cararray As New ArrayList

Dim icounter As Integer

Dim fv As streamreader = file.opentext("cars.txt") ' assume that file is in
bin dir!

Dim carstring() As String

Do

Dim car As New automobile

carstring = fv.ReadLine.Split(","c)

car.make = carstring(0).Trim

car.model = carstring(1).Trim

cararray.Add(car)

icounter = icounter + 1

Loop Until fv.peek = -1

ListBox1.DataSource = cararray

ListBox1.DisplayMember = "model"

TextBox1.DataBindings.Add("Text", cararray, "model")

TextBox2.DataBindings.Add("Text", cararray, "make")

End Sub



The class

Public Class automobile

Dim mstrmake As String

Dim mstrmodel As String

Public Property make() As String

Get

Return mstrmake

End Get

Set(ByVal Value As String)

mstrmake = Value

End Set

End Property

Public Property model() As String

Get

Return mstrmodel

End Get

Set(ByVal Value As String)

mstrmodel = Value

End Set

End Property

End Class



Ken
 
Back
Top