Ashish
Ashish

Reputation: 75

How to display a Dictionary in a ListBox

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

Upvotes: 2

Views: 14696

Answers (4)

Adam Ruth
Adam Ruth

Reputation: 3655

Use the Format event on the ListBox:

#1). Register the event:

myListBox.Format += myListBox_Format;

#2). Handle the event. Set Value property of the passed-in ListControlConvertEventArgs to the string to be displayed for a list item. According to Microsoft's documentation of the Event: "The Format event is raised before each visible item in the ListControl is formatted".

private void myListBox_Format(object sender, ListControlConvertEventArgs e)
{
    KeyValuePair<string, int> item = (KeyValuePair<string, int>)e.ListItem;
    e.Value = string.Format("{0}({1})", item.Key, item.Value);
}

Upvotes: 8

AppDeveloper
AppDeveloper

Reputation: 937

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()

Upvotes: 0

Brandon Grossutti
Brandon Grossutti

Reputation: 1271

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

public partial class tester : Form
{
    public tester()
    {
        InitializeComponent();
         List<MyObject> myObjects = new List<MyObject>();
        MyObject testObject = new MyObject("A", "10");
        myObjects.Add(testObject);
       BindingSource bindingSource = new BindingSource(myObjects,null);
        listBox1.DisplayMember = "DisplayValue";
        listBox1.DataSource = bindingSource;
    }
}

public  class MyObject
{
    private string _key;
    private string _value;

    public MyObject(string value, string key)
    {
        _value = value;
        _key = key;
    }

    public string Key
    {
        get { return _key; }
    }

    public string Value
    {
        get { return _value; }
    }

    public string DisplayValue
    {
        get { return string.Format("{0} ({1})", _key, _value); }
    }
}

Upvotes: 5

mickyjtwin
mickyjtwin

Reputation: 4990

You could iterate over the dictionary object and build the listbox item as you go.

  foreach (KeyValuePair<string, int> kvp in myDictionary)
  {
      lbx.Items.Add(String.Format("{0}({1})", kvp.Key, kvp.Value.ToString()));
  }

Upvotes: 0

Related Questions