Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Item data in ListBox (Windows Forms)

Posted on 2004-03-29
3
Medium Priority
?
999 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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 …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

688 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