ListBox and class instances

  • Thread starter Thread starter Mr. Bean
  • Start date Start date
M

Mr. Bean

Hello,
I'm making a small app that loads users from an xml file. After loading
the data, new instances of a custom users class are initalized and at
the same time a listbox items are initialized with the users. My
question is when the listbox is changed how could I update a property
or know that Item in the list box, which instatnce it references.

//code to load users into usersDS
........
users[] theUsers = new users[anything.Count];
int counter = 0;
foreach(datarow user in userDS){

theUsers[counter] = new user(proper1, prop2);
listbox1.items.add(theUsers[counter].toString());
counter++;
}
listbox1_OEvent(){
//which users instance I'm I?
}
How do I Know which user instancce does the selected item represents?
 
Mr. Bean said:
Hello,
I'm making a small app that loads users from an xml file. After loading
the data, new instances of a custom users class are initalized and at
the same time a listbox items are initialized with the users. My
question is when the listbox is changed how could I update a property
or know that Item in the list box, which instatnce it references.

//code to load users into usersDS
.......
users[] theUsers = new users[anything.Count];
int counter = 0;
foreach(datarow user in userDS){

theUsers[counter] = new user(proper1, prop2);
listbox1.items.add(theUsers[counter].toString());

There is a better way than this - just add the users objects
*themselves* to the listbox. A listbox automatically *displays* the
..toString() of its contents.
counter++;
}
listbox1_OEvent(){
//which users instance I'm I?
(users)listbox1.SelectedItem

}
How do I Know which user instancce does the selected item represents?

By adding the actual users objects this is made simple - the selected
item *is* a users object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top