Solved

How to bind a listbox with a Class?

Posted on 2007-12-05
8
434 Views
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?

regards
0
Comment
Question by:Dnx_7
[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
  • 2
  • 2
  • +1
8 Comments
 
LVL 13

Accepted Solution

by:
Michael_D earned 125 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.
0
 
LVL 21

Expert Comment

by:surajguptha
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
0
 
LVL 2

Author Comment

by:Dnx_7
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...

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

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

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

Expert Comment

by:Michael_D
ID: 20411830
Dim p as Person = BusinessLayer.GetPerson

lb.Add(p)

......

in your DoubleClick event

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

Access all properties of selected person

Console.Write(p.FirstName)
Console.Write(p.Name)




0
 
LVL 2

Author Comment

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

i didn't see it sorry

thank you to both of you !
0
 
LVL 2

Author Closing Comment

by:Dnx_7
ID: 31412856
Excellent!
0
 
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
MessageBox.Show(obj.Name);
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL and CLR parameter strings 9 66
CSS question 16 63
Datatable / Dates ? 4 32
Creating a Windows C++ program in visual studio 2015 5 31
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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