Original Post
I'm trying to write an app in C#. I'm trying to present a list box which contains a list of text strings and a list of hidden index values.
I've got something like this in code:
The problem is that the Items collection is a list of generic objects and C# tries its best to print out the object. I really want the Key to be the hidden value and the Value to be the displayed list item. However, if I just display the value, I lose the key and can't index into my Dictionary data structure.
Ideally, there should be an overloaded function on the Listbox.Items.Add() method which accepts a display value and a hidden value. Alas, I'm dumb and stuck to hacks and inelegant trickery to get around this.
I've come up with the ugliest, most inelegant hack to get around this (almost worthy of DailyWTF). It involves creating a helper class to be a container for two strings, an array list since it can be datasourced, setting the listbox datasource to the array list, and setting the DisplayMember and ValueMembers to the helper class strings. It works but I'm so disgusted by this garbage that I'm begging for help. What's the right way to do this?
I've got something like this in code:
Dictionary<Guid, MyObject> data; //filled with stuffListBox myListbox = new ListBox();foreach(KeyValuePair<Guid, MyObject> kvp in data){ myListbox.Items.Add(kvp);}The problem is that the Items collection is a list of generic objects and C# tries its best to print out the object. I really want the Key to be the hidden value and the Value to be the displayed list item. However, if I just display the value, I lose the key and can't index into my Dictionary data structure.
Ideally, there should be an overloaded function on the Listbox.Items.Add() method which accepts a display value and a hidden value. Alas, I'm dumb and stuck to hacks and inelegant trickery to get around this.
I've come up with the ugliest, most inelegant hack to get around this (almost worthy of DailyWTF). It involves creating a helper class to be a container for two strings, an array list since it can be datasourced, setting the listbox datasource to the array list, and setting the DisplayMember and ValueMembers to the helper class strings. It works but I'm so disgusted by this garbage that I'm begging for help. What's the right way to do this?