Solved

Item data in ListBox (Windows Forms)

Posted on 2004-03-29
3
988 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 250 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now