Collection to listbox

B

Brian Henry

I have a simple problem... I am makeing a journal which has a collection of
entries (journal class and entries class).. when you select a journal i need
it to show in a listbox all the entries (the title of the entry) which is
easy.. but I also need an ID for the item so i can tie to to a database
entry.. is there any way to bind to a collection? or better yet set a value
for each item in a listbox kinda like data binding does with the value
member.. thanks
 
H

Herfried K. Wagner [MVP]

* "Brian Henry said:
I have a simple problem... I am makeing a journal which has a collection of
entries (journal class and entries class).. when you select a journal i need
it to show in a listbox all the entries (the title of the entry) which is
easy.. but I also need an ID for the item so i can tie to to a database
entry.. is there any way to bind to a collection? or better yet set a value
for each item in a listbox kinda like data binding does with the value
member.. thanks

You can set 'DataSource', 'DisplayMember' and 'ValueMember'.
 
H

Herfried K. Wagner [MVP]

* "Brian Henry said:
but to a collection? how would i do that?

If the data is, for example, stored in an arraylist:

\\\
With Me.ListBox1
.DisplayMember = "Name"
.ValueMember = "Age"
.DataSource = Database.FindPeople(...)
End With
///
 
J

Jay B. Harlow [MVP - Outlook]

Brian,
As Herfried stated, you set the 'DataSource', 'DisplayMember' and
'ValueMember' properties of the ListBox.

Have you tried something like:
Dim list As ArrayList
Dim lb As ListBox
lb.DisplayMember = "property1"
lb.ValueMember = "property2"
lb.DataSource = list

Where Property1 & Property2 are properties of the items in the collection,
if you do not set DisplayMember & ValueMember, then the ToString method of
the objects in the collection will be used.

For further details see the ListBox.DataSource (inherited from
ListControl.DataSource) property in MSDN.

Hope this helps
Jay

Brian Henry said:
but to a collection? how would i do that?


collection
i
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top