简体   繁体   中英

How to add value to ComboBox's items, so I can choose it like this comboBox.SelectedIndex = myInt?

Let's say I have this items:

        comboBox.Items.Add("Access"); // make it equal to 31
        comboBox.Items.Add("Create"); // make it equal to 34
        comboBox.Items.Add("Delete"); // make it equal to 36
        comboBox.Items.Add("Modify"); // make it equal to 38

Now, I call

comboBox.SelectedIndex = 34; // want to "Create" item has been chosen

What is the easiest way to do that ?

Unfortunately, winforms doesn't have a ListItem class like ASP.NET does, so you'll have to write your own:

public class cbxItem
{
public string text {get;set;}
public int id {get;set;}

 public override string ToString() 
 { 
      return text;
 }
// you need this override, else your combobox items are gonna look like 
// "Winforms.Form1 + cbxItem"
}

then add items to your combobox like this:

cbxItem item = new cbxItem();
item.text = "Access";
item.id = 31;    
comboBox.Items.Add(item); 

To get the "id" or "value" or however you wish to call it:

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
       var cbxMember = comboBox1.Items[comboBox1.SelectedIndex] as cbxItem;

      if (cbxMember != null) // safety check
       {
       var value = cbxMember.id; 
       }
    }

It depends a lot on how your data is going to be managed.

If your items are not going to be modified over the course of your program you can simply use a dictionary as a mapping table.

comboBox.Items.Add("Access"); // make it equal to 31
comboBox.Items.Add("Create"); // make it equal to 34
comboBox.Items.Add("Delete"); // make it equal to 36
comboBox.Items.Add("Modify"); // make it equal to 38

Dictionary<int, int> mapTable = new Dictionary<int, int>();
mapTable.Add(31, 0);
mapTable.Add(34, 1);
mapTable.Add(36, 2);
mapTable.Add(38, 3);

Then simply use the following:

comboBox.SelectedIndex = mapTable[34];

You can even put this logic in a class that inherits from ComboBox for better abstraction.

You want to use SelectedValue instead of SelectedIndex . The index is just a count (0,1,2,3...). The value can be specified.

You'd need to add something more complex than a simple string to do what you want. If all you want is an int and an associated string then you could use a KeyValuePair, but any custom object would work. You'd then need to set DisplayMember and ValueMember on the comboBox so that it displays correctly. Oh, and use SelectedValue or SelectedMember instead of SelectedIndex.

Here's your add:

comboBox.Items.Add(new KeyValuePair<int, string>(){ Key = 34, Value = "Access"});

Yeah, a custom object would make that a simpler statement but the concept is the same.

comboBox.Items.Add(new WorkItem { Key = 31, Value = "Access" });
comboBox.Items.Add(new WorkItem { Key = 34, Value = "Create" });
comboBox.Items.Add(new WorkItem { Key = 36, Value = "Delete" });
comboBox.Items.Add(new WorkItem { Key = 38, Value = "Modify" }); 
selectItemByKey(34);

You need to add this method:

   private void selectItemByKey(int key)
   {
        foreach (WorkItem item in comboBox.Items)
        {
            if (item.Key.Equals(key))
                comboBox.SelectedItem = item;
        }
   }`

And a Class like this:

public class WorkItem
{
    public int Key { get; set; }
    public string Value { get; set; }

    public WorkItem()
    {
    }

    public override string ToString()
    {
        return Value;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM