By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

How do I use an arraylist and a structure together?

P: n/a
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

---
Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <rbcc@-NOSPAM-vei.net> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
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

---
Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest

Community Website: http://www.dotnetjunkies.com/newsgroups/
Nov 20 '05 #2

P: n/a
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.selectedinde x),auto).mode
end su

will this work
Joh


---
Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest Community Website: http://www.dotnetjunkies.com/newsgroups/
Nov 20 '05 #3

P: n/a
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
---------------
"RBCC" <rbcc@-NOSPAM-vei.net> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
something like this:

file to read: cars.txt
subaru, outback
chevy,nova
ford ,mustang

imports system.collection
imports system.io

structure auto
dim make as string
dim model as string
end structure

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

end class

Private Sub lstcars_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lstNames.SelectedIndexChanged
txtmake.text=ctype(arraylist(lstcars.selectedindex ),auto).make
txtmodel.text=ctype(arraylist(lstcars.selectedinde x),auto).model
end sub

will this work?
John



---
Posted using Wimdows.net NntpNews Component - Posted from .NET's Largest
Community Website: http://www.dotnetjunkies.com/newsgroups/

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.