Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

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?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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...
Avatar of IzzyTwinkly

ASKER

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?
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

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Mike,

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

Izzy