1

Suppose I have a fixed set of identifiers organized in, for example, a dictionary.

public static Dictionary<int, string> clientIds = new Dictionary<int, string>()
{
    { 0, "aa" },
    { 1, "ab"  },
    { 2, "ac" },
    { 3, "ba" },
    { 4, "bb" }
};

Then, I dynamically, at runtime, add to a ComboBox "friendly names" that are related to those identifiers (I don't know, however, which ones will be added.

clientIdCombo.Add(friendlyName);

Suppose that, at index zero, the friendly name "Alpha Beta" is added. That would relate to "ab" identifier. How can I know the user has selected the "ab" identifier, without having to condition based off the text displayed on the combo to the user? I tried using a BindingList instead, but that only provides me with that displayed text as well.

It sounds like something simple - how can I add underlying data to each ComboBox index? The simplest possible approach would be preferable, though multiple solutions are welcome.

Thanks.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Just use selected item index as a dictionary key... – apocalypse Mar 15 '17 at 19:07
  • See http://stackoverflow.com/questions/6412739/binding-combobox-using-dictionary-as-the-datasource#6412893 – tjcertified Mar 15 '17 at 19:08
  • Possible duplicate of [Binding Combobox Using Dictionary as the Datasource](http://stackoverflow.com/questions/6412739/binding-combobox-using-dictionary-as-the-datasource) – tjcertified Mar 15 '17 at 19:08
  • @apocalypse But I don't know at what index an item will be added, this happens at runtime based on user input. – Johnson Whitler Mar 15 '17 at 19:10
  • @JohnsonWhitler: then try this: http://stackoverflow.com/questions/3063320/combobox-adding-text-and-value-to-an-item-no-binding-source – apocalypse Mar 15 '17 at 19:10
  • Where is your friendlyname stored and how it is related to the ids? idon't see it there? do you have some object that contain both the friendly_name and the ids? – Ofir Winegarten Mar 15 '17 at 19:12

2 Answers2

1

ComboBox items may be of any type you like and the items' display name would be defined by this type's ToString method.

This means you may define your item type like this:

class ClientItem
{
    public int Index;
    public string Id;
    public string FriendlyName;
    public override string ToString()
    {
        return FriendlyName;
    }
}

And fill your combo box with instances of this class.

comboBox.Items.Add(new ClientItem { 
    Index = 1,
    Id = "ab",
    FriendlyName = "Alpha Beta",
});

Then you can use the combo box items accessing all item's data. Just remember to cast to the specific item type:

var client = comboBox.SelectedItem as ClientItem;
MessageBox.Show(client.Id + ": " + client.FriendlyName);
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • Great solution, thanks. Alternatively, is it possible to simply assign ComboBox.DisplayMember and ComboBox.ValueMember to a generic variable? – Johnson Whitler Mar 15 '17 at 20:18
1

Just add as many items to the ComboBox as there are in the array, each with its corresponding text and in the same order that they are on the array. Then, when you want to get the selected item from the array, get the selected index using ComboBox.SelectedIndex and get the item from the array corresponding to that index. In your case you are using a int-indexed dictionary, which behaves like an array in terms of indexing it.

TLDR:

string[] array = new[] { "aa", "ab", "ac" };

//This array is "equivalent" to
Dictionary<int, string> dic= new Dictionary<int, string>()
{
    { 0, "aa" },
    { 1, "ab" },
    { 2, "ac" },
};

//Add the items to your ComboBox, for simplicity let's use this
ComboBox.Items.Add("Alfa Alfa");
ComboBox.Items.Add("Alfa Bravo");
ComboBox.Items.Add("Alfa Charlie");

//Later, when retrieving the selected item
int selIndex = ComboBox.SelectedIndex;
string selItem = array[selIndex];
Felipe
  • 336
  • 8
  • 19
  • That's an approach in case you're sure to add ALL the items you have on the ComboBox. In my scenario, I don't know which will be added, for example "Alfa Alfa" may not be added, but I won't know that until runtime, which makes retrieval based upon SelectedIndex alone impossible. – Johnson Whitler Mar 15 '17 at 20:15