I'm trying to display Key/Value Pairs from a Dictionary to a ListBox.
Key Value
A 10
B 20
C 30
I want to display them in a ListBox in following format
A(10)
B(20)
C(30)
Using following code I have been able to link Listbox.Datasource to Dictionary.
myListBox.DataSource = new BindingSource(myDictionary, null);
Its being displayed as
[A, 10]
[B, 20]
[C, 30]
I can't figure out how to format it so that it is displayed in the way I want.
Any help will be appreciated.
Thanks Ashish
You could iterate over the dictionary object and build the listbox item as you go.
Actually if you would like to customize list box derive from it and override
protected override OnDrawItem
Answer 1 will get what you stated in the question but if you want to reflect changes in the objects it would be better if you wrote the drawing routine so it would automatically reflect.
or you can change Text of item which would also do the trick.
Don't forget to call BeginUpdate() and EndUpdate()
For proper long term flexiblitity I would try and use a typed object, then you can do what ever you like later, raise events, change the values, not have to use unique keys, get a real object from listbox rather than just a formatted string
Use the Format event on the list box: