Looking for good windows forms sample in C# showing how to work with ListBox

G

Grant Schenck

Hello,

This seems like it should be easy...

I have a listbox on a .NET form.

I add a new item to the list box.

How can I associated a separate data value?

Can I add (and retrieve) an arbitrary data value independent of the text
displayed?

I also tried creating an object and providing a ToString override. That did
let me add the item but I still didn't see a way to lookup based on
enumerating the list...

Thanks,
 
K

Kevin Yu [MSFT]

Hi Grant,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to display a column in the
data source, and retrieve another column in the data source as value. If
there is any misunderstanding, please feel free to let me know.

As far as I know, this can be achieved with ListBox's DisplayMember and
ValueMember property. Thes two properties get or set a string that
specifies the property of the data source whose contents you want to
display and the data source from which to draw the value.

You can check the following link for more information.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbtskdatabindingcomboboxcheckedlistboxorlistboxcontrol.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

Joshua Flanagan

You were on the right track with your second approach. Create a class
that has a "Text" property and a "Data" property, or whatever you want
to call them (ex: UserName and UserID). On the ListBox, set the
DisplayMember to point to your Text property. Set the ValueMember to
point to your Data property.
 
G

Grant Schenck

No, this appears to relate to tying a listbox to a database.

My situation is that I need to show a text string which can change over time
for each entry in the list box. In addition, the "key" to each item is an
unique integer value. So, when they select or double click an item I want
to be able to determine what the associated integer value is for the item.
However, the Items collection is a collection of objects. There are no
methods to set or retrive a data value.

Similarly, I need to be able to find a specific item in the collection based
on the data value.
 
G

Grant Schenck

This seems to be the way to go...

If I create a class with two members, an int and a string and then provide a
ToString override which returns my string, I guess that would work.

Then, if I want to determine the int value associated with a given Listbox
item I can cast the object to my class and access the members.

However, how are the DisplayMember and ValueMember used? Those values apply
to the whole ListBox.

Also, while there seems to be a web version of the ListBox that contains a
collection of ListItems, this isn't true for the Windows Form version of
ListBox, correct? That contains a collection of objects, correct?
 
J

Joshua Flanagan

Correct - the web version of ListBox is different in that it holds
ListItems instead of objects.

And you are correct that you can retrieve specific values of an object
by casting it to the appropriate type.
 
K

Kevin Yu [MSFT]

Hi Grant,

I agree with Joshua. Object collection binding is similar to DataSet
binding. We can set "Text" as DisplayMember and "Data" as ValueMember in
this case. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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