I got another problem with my ListBox.
I have a class Entity with string Forename, Surname.
Now I would like to show the user every Entity in a ListBox personList.
Therefor I do this:
foreach(Entity e in EntityHandler.entityList)
{
personList.Items.Add(e.Name);
}
The user can select multiple entries of personList.
After clicking a button the application shall evaluate every selected entry in a foreach-loop.
For this I simply use
foreach(string selected in personList.SelectedItems)
{
//do some stuff
}
The problem is now that there could be multiple Entity instances with exactly the same values.
The only static difference between every Entity is its UID but I don’t want to write this ugly UID into their entries.
Is there a way how I could get the corresponding Entity out of the string value that I get from SelectedItems?
I read about overriding GetHashCode() and Equal() but I didn’t get an idea why this should work?
Thanks in advance!
You should bind actual entitys to ListBox instead only names and set DisplayMemeber property of ListBox to “Name” property of Entity. This will give you direct access to ListBox items – Entities.
Take a look at this link on how to bind objects to ListBox:
http://sharpertutorials.com/list-box-data-binding/