Referring selected items' property in C# winform.

Hi guys,

I have PeopleList which is IEnumerable type.
When a Form loads, I use the following code to add each person to the listbox, myListBox.

foreach(var person in PeopleList)
{
      myListBox.Items.Add(person.Name);
}

Now all the people's names are populated in the listbox when a form is opened.
However, I used "person.Name" for user-friendly item names.
What I really need is "person.id", not "person.Name".

So when I click the button, I want to refer all the selected people's ids.
Can you please tell me how to get the list of selected people's ids?
IzzyTwinklyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
What I really need is "person.id", not "person.Name".

Don't add "person.Name", simply add the "person" instance directly into the ListBox.  Now set the DisplayMember() Property of the ListBox to "Name", and the ValueMember() Property to "id".  Consequently, the "Name" property of the person will be displayed, and their "id" will be returned in the SelectedValue() property when something is selected.  Additionally, you can cast the SelectedItem() property to whatever type "person" is and access all of its members...
0
IzzyTwinklyAuthor Commented:
Hi Mike,

Thanks for your help.

private void Form_Load(object sender, EventArgs e)
{            
            foreach (var person in PeopleList)
            {                
                myListBox.Items.Add(person);
                myListBox.DisplayMember = "Name";
                myListBox.ValueMember = "Id";                
            }                
}
it still shows each person's name in myListBox when the form is loaded, which is good.
Now when I click the button, all the selected people's "Id"s should be displayed. and this is what I did.
 private void btn1_Click(object sender, EventArgs e)
        {
                string result = "";
                foreach (var item in myListBox.SelectedItems)
                {                    
                    result += item as string;
                }
                MessageBox.Show(result);                
        }
   
And Nothing is displayed in the message box.
Can you please let me know what I did wrong to fix this issue?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You're casting the "Person"? type stored in the ListBox to a string, which it's not.  Simply access the "ID" property directly thru your "item" variable:
        private void btn1_Click(object sender, EventArgs e)
        {
                string result = "";
                foreach (var item in myListBox.SelectedItems)
                {                    
                    result += item.Id + Environment.NewLine;
                }
                MessageBox.Show(result);                
        }

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

IzzyTwinklyAuthor Commented:
Hi Mike,

I wish I could do that. When I enter item. only "Equals", "GetHashCode", GetType", and "ToString" are appeared in intellisense.
I don't know if this will help, but I have "Person" class that has "Name" and "ID"
Also the type of PeopleList is IEnumerable<Person>

Thanks,
Izzy
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Try casting to "Person":
        private void btn1_Click(object sender, EventArgs e)
        {
                string result = "";
                foreach (var item in myListBox.SelectedItems)
                {                    
                    result += ((Person)item).Id + Environment.NewLine;
                }
                MessageBox.Show(result);                
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IzzyTwinklyAuthor Commented:
Hi Mike,

Thanks for  your perfect solution as always.
I really appreciate your help!!!

Izzy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.