简体   繁体   English

如何为ComboBox的项目增加价值,以便像这样的comboBox.SelectedIndex = myInt选择它?

[英]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: 不幸的是,winforms不像ASP.NET那样具有ListItem类,因此您必须编写自己的类:

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: 要获取“ id”或“ value”,或者您想调用它:

 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. 您甚至可以将此逻辑放在从ComboBox继承的类中,以获得更好的抽象。

You want to use SelectedValue instead of SelectedIndex . 您要使用SelectedValue而不是SelectedIndex The index is just a count (0,1,2,3...). 索引只是一个计数(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. 如果只需要一个int和关联的字符串,则可以使用KeyValuePair,但是任何自定义对象都可以使用。 You'd then need to set DisplayMember and ValueMember on the comboBox so that it displays correctly. 然后,您需要在comboBox上设置DisplayMember和ValueMember,以使其正确显示。 Oh, and use SelectedValue or SelectedMember instead of SelectedIndex. 哦,请使用SelectedValue或SelectedMember而不是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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ComboBox.SelectedIndex添加为FK - How to add ComboBox.SelectedIndex as FK comboBox.SelectedIndex保持自动删除 - comboBox.SelectedIndex keep getting deleted automatically WinForms-清除ComboBox.SelectedIndex导致错误 - WinForms - Clearing ComboBox.SelectedIndex Causing Error 设置ComboBox.SelectedIndex会破坏ComboBox.SelectedItem上的OneWayToSource绑定 - Setting ComboBox.SelectedIndex Breaks OneWayToSource Binding On ComboBox.SelectedItem 如何等待为comboBox创建句柄,以便可以更改selectedIndex? - How do I wait for a handle to be created for a comboBox, so that I can change the selectedIndex? 我如何循环并将项目添加到comboBox,使其从1开始到8? - How do i loop and add the items to comboBox so it will start from 1 to 8? 当我使用 Win32 API 和 P/Invoke 更改 C# WinForms ComboBox.SelectedIndex 时,如何触发 SelectedIndexChanged? - How do I make SelectedIndexChanged trigger when I change C# WinForms ComboBox.SelectedIndex using Win32 API and P/Invoke? 为什么设置comboBox.SelectedIndex无法更改在ComboBox的可编辑部分中显示的内容? - Why does setting comboBox.SelectedIndex fail to change what appears in the editable portion of my ComboBox? 如何将不同的selectedindex组合框值插入数据库? - How to insert a different selectedindex combobox value into database? 如何将字典分配给组合框的项目(显示和值成员)? - How can I assign a Dictionary to a combobox's Items (Display and Value Members)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM