How to bind a listbox with a Class?

Posted on 2007-12-05
Medium Priority
Last Modified: 2013-11-07
Hi experts

i have a class with 2 properties
eg : Name and firstname

in my application this class is sent from business services and i have to add it to my listbox in the GUI

so when each time i'll receive the class (name and firstname) from business layer, i have to show in my listbox the FIRSTNAME and put in the item tag or wherever else the NAME to keep reference

the goal is when the user will double_click on the item, i can retrieve the NAME from the FIRSTNAME clicked

can you help me?

Question by:Dnx_7
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
  • 2
  • 2
  • +1
LVL 13

Accepted Solution

Michael_D earned 500 total points
ID: 20411694
in your class override ToString method to return whatever you want to be shown in the list box
then Add the WHOLE object to the list box - you don't have to store refference in the Tag property.
LVL 21

Expert Comment

ID: 20411706
listbox.DisplayMember = "FirstName";
listbox.ValueMember = "Name";
listbox.DataSource = class object returned from the web service

on click of the button
"listbox.SelectedValue" will give the name

Author Comment

ID: 20411755
doesn't work...

i got exception... and plus, i cannot use datasource because i have to keep the previous data...

it's like the business layer will send step by step

first notification
myApp <== business will send ("david", "nameOfDavid")

second notification
myApp <== business will send ("Henry", "nameOfHenry")

but i have to keep david from the first notification...

 Dim pf As PluginFounded = DirectCast(e, PluginFounded)
            lb.DisplayMember = "Interface"
            lb.ValueMember = "Path"
            lb.DataSource = pf

Open in new window

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

LVL 21

Expert Comment

ID: 20411814
We will work on the keeping previous data later. But what exception are you getting?
LVL 13

Expert Comment

ID: 20411830
Dim p as Person = BusinessLayer.GetPerson



in your DoubleClick event

Dim p as Person = DirectCast(lb.SelectedItem, Person)

Access all properties of selected person



Author Comment

ID: 20411840
that's ok, the Michael_D's solution is correct!

i didn't see it sorry

thank you to both of you !

Author Closing Comment

ID: 31412856
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20411949
Michael_D suggestion is better, don't use data binding just add the entire object instead of a string:

class MyBusinessClass
     string Name;
     string FirstName;
     public override string ToString()
            return FirstName;  // this is what you will see in listbox

MyBusinessClass obj;  // fill it somewhere
listbox1.Items.Add(obj);   // add the entire object, not a string

// .... later

MyBusinessClass obj = (MyBusinessClass)listbox.SelectedItem;    // casting is required

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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