Jane Dallaway

Jane Dallaway

Jane Dallaway  //  Data loving developer/leader/product shaper, storyline curator/creator, life-long learner, photographer, dog owner, reader, crafter, gardener and occasional snowboarder

This blog contains all sorts of odds and ends, from event reviews, stuff about my storyline project, bits of craft, through thoughts on learning, to photography stuff, and general inspiration things. It's a bit all over the place with no real theme, but then so am I!

Email: jane @ dallaway.com
Also at:    

C# - Using Objects in ListBoxes/ComboBoxes

As I become more "object oriented" in my development approach (I've done VB for too long) I was struggling to get ComboBoxes and ListBoxes to accept and display an object appropriately rather than just populating the list with a string, and then using an index or string lookup to find the appropriate item.

The other day I had a sniff around the internet and thought that the Format event might help, and today I managed to get it to work.

If I have a combobox called myList, and I've assigned a Person object to it as
myList.Items.Add(person). I then need to register the event Format as:

private void myList_Format(object sender, ListControlConvertEventArgs e)
{
e.value = ((Person)e.ListItem).Surname;
}

myList will then be populate with the Surname of the Person, but the myList.SelectedItem will hold the object currently selected.

0 comments

Leave a comment...