简体   繁体   English

通过键而不是按值设置ComboBox选定项c#

[英]set ComboBox selected item by Key not by value c#

Before post this question ,I believe it is simple issue , I search for the answer and didn't find suitable solution. 在发布此问题之前,我认为这是一个简单的问题,我正在寻找答案,却找不到合适的解决方案。

in my daily work , I am working with web applications and can easily get or set values of dropdownlists 在日常工作中,我正在使用Web应用程序,并且可以轻松获取或设置下拉列表的值

i can not do the same in windows application C# 我不能在Windows应用程序C#中执行相同的操作

I have combobox and class comboItem 我有combobox和class comboItem

 public class ComboItem
    {
        public int Key { get; set; }
        public string Value { get; set; }
        public ComboItem(int key, string value)
        {
            Key = key; Value = value;
        }
        public override string ToString()
        {
            return Value;
        }
    }

Say the combobox is binded through hard code and the values are 假设组合框通过硬代码绑定,并且值是

  • Key : 1 / Value : Male 钥匙:1 /价值:男性
  • Key : 2 / Value : Female 钥匙:2 /价值:女性

  • Key : 3 / Value : Unknown 键:3 /值:未知

lets say i have the Key =3 and I want to set this item ( whose key is 3 ) through code so when form is loaded , the selected value by default will be Unknown. 可以说我有Key = 3,并且我想通过代码设置此项(其key为3),因此在加载form时,默认情况下选定的值为Unknown。

combobox1.selectedValue =3 //Not Working , selectedValue used to return an object
combobox1.selectedIndex = 2 //Working as 2 is the index of key 3/Unknown

but lets say i don't know the index , how can i get the index of item whose key = 3 ? 但是可以说我不知道​​索引,我如何获取键= 3的项的索引?

index can be get through value in this way 索引可以通过这种方式获得价值

int index = combobox1.FindString("Unknown") //will return 2

FindString take a value not a key , i need something like FindString which take a key and return index FindString取值而不是键,我需要像FindString这样的东西,它取一个键并返回索引

NOTE : Here is how i bind my drop down menu 注意:这是我绑定下拉菜单的方式

 JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                        jsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;

                        var empResult= await response.Content.ReadAsStringAsync();
                        List<Emp> Emps= JsonConvert.DeserializeObject<Emp[]>(empResult, jsonSerializerSettings).ToList();
                        foreach (var item in Emps)
                        {
                            ComboItem CI = new ComboItem(int.Parse(item.ID), item.Name);
                            combobox1.Items.Add(CI);
                        }
                        this.combobox1.DisplayMember = "Value";
                        this.combobox1.ValueMember = "Key";

You need to set the ValueMember property so the ComboBox knows what property to deal with when SelectedValue is being used. 您需要设置ValueMember属性,以便ComboBox知道使用SelectedValue时要处理的属性。 By default the ValueMember will be empty. 默认情况下, ValueMember将为空。 So when you set the SelectedValue , the ComboBox does not know what you want to set. 因此,当您设置SelectedValueComboBox不知道您要设置什么。

this.comboBox1.ValueMember = "Key";

Normally, you would also set the DisplayMember property: 通常,您还可以设置DisplayMember属性:

this.comboBox1.DisplayMember = "Value";

If you do not set it, it will just call ToString() on the object and display that. 如果不设置它,它将仅在对象上调用ToString()并显示它。 In your case the ToString() returns the Value . 在您的情况下, ToString()返回Value

how can i get the index of item whose key = 3 ? 如何获得键= 3的项的索引?

If you want the item whose key is 3, why do you need to get it from the combobox? 如果您想要键为3的项目,为什么需要从组合框中获取它? You can just get it from the collection the combobox is bound to: 您可以从组合框绑定到的集合中获取它:

For example, imagine this: 例如,想象一下:

var items = new List<ComboItem> { new ComboItem(1, "One"),
    new ComboItem( 2, "Two") };

this.comboBox1.DataSource = items;
this.comboBox1.DisplayMember = "Value";
this.comboBox1.ValueMember = "Key";

this.comboBox1.SelectedValue = 2;

If I need the item whose key is 2, then this will achieve that: 如果我需要密钥为2的项目,则可以实现以下目的:

// Use Single if you are not expecting a null
// Use Where if you are expecting many items
var itemWithKey2 = items.SingleOrDefault(x => x.Key == 2);

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

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