Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Item data in ListBox (Windows Forms)

Posted on 2004-03-29
3
Medium Priority
?
1,001 Views
Last Modified: 2012-08-13
How can I keep item data associated with ListBox item?
I expected to have the Tag property, but ListBox.ObjectCollection contains Object members.
0
Comment
Question by:AlexFM
3 Comments
 
LVL 48

Author Comment

by:AlexFM
ID: 10703416
For example, ListView.ListViewItemCollection Class contains ListViewItem members, and I can use ListViewItem.Tag Property to keep data associated with ListView item. What can I do with ListBox?
0
 
LVL 15

Accepted Solution

by:
SRigney earned 1000 total points
ID: 10703712
The good thing about a ListBox is that you can add objects and tell it what to display.  Then any additional properties that you want are already associated with the record, you just need to cast it when you get it out.

For example, let's say I have a class, clsMyItem, the ToString has been overriden  with the data I want displayed.

I can Add the object to the listbox, and when I want to get it out I just do
clsMyItem CurrentItem = (clsMyItem)ListBox1.SelectedItem

Now I have access to all of the properties that were on the original object that was added.  Which is what is usually really wanted with the ItemData property anyway.
0
 
LVL 10

Expert Comment

by:EBatista
ID: 10703813
use the pair DisplayMember and ValueMember properties instead.
ie. if this is a database app you could store in the ValueMember the primary key of a table and in DisplayMember the text that users see in the control:

ListBox1.DataSource=tableUsers
ListBox1.DisplayMember="UserName"
ListBox1.ValueMember="UserID"

then to retrieve the primary key value corresponding to the selected item you can use the SelectedValue property:

string strKey=ListBox1.SelectdValue.ToString()

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question