zoukankan      html  css  js  c++  java
  • combox的键值对应用: KeyValuePair<TKey, TValue>

    combox的item列表里存在字符,如何将多个字符与序号0123。。。对应?
    键值对  KeyValuePair<TKey, TValue> 就很好地帮我们解决了问题。

    举个栗子:

    private void Form1_Load(object sender, EventArgs e)
            {
    
                setComboxItemData(comboBox1,new int[]{0,1,2,3,4,5});
            }
     private void setComboxItemData(ComboBox cb, int[] data)
            {
                cb.DisplayMember="key";
                cb.ValueMember = "value";
                System.Diagnostics.Debug.Assert(cb.Items.Count==data.Length);
                List<KeyValuePair<string, int>> listItem= new List<KeyValuePair<string, int>>();;
                for (int i = 0; i < cb.Items.Count; i++)
                {

                      //KeyValuePair<string, int> Item = new KeyValuePair<string,int>(cb.Items[i].ToString(),data[i]);
                      //cb.Items[i] = Item;

                    listItem.Add(new KeyValuePair<string,int>(cb.Items[i].ToString(), data[i]));

                }
                cb.DataSource = listItem;
               
                System.Diagnostics.Debug.Assert(cb.DataSource != null);
            }
     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                KeyValuePair<string,int> key=(KeyValuePair<string,int>)(comboBox1.SelectedItem);
                int index = key.Value;
                string name = key.Key;
            }

    这样:在comboBox1的selected事件中 通过读取选择的item 就可知道选择了第几项

  • 相关阅读:
    linux之sed用法
    vim 设置tab空格个数
    centos 7远程登陆win10
    linux find命令学习
    CENTOS 7 修改默认启动内核
    Centos7更改默认启动模式
    centos 7创建桌面快捷方式
    修改centos中文为英文显示
    正则的sub
    超时或错误重试
  • 原文地址:https://www.cnblogs.com/zhayunjia/p/6594005.html
Copyright © 2011-2022 走看看