zoukankan      html  css  js  c++  java
  • comboBox绑定字典Dictionary 获取value中的值

    第一种 最简洁的方法

    Dictionary<string, string> list = new Dictionary<string, string> { {"this is i1", "001"}, {"this is i2", "002"} };

    private void Form1_Load(object sender, EventArgs e) {

    comboBox1.Items.AddRange(list.Keys.ToArray());

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {

    string a = list[comboBox1.SelectedText];

    textBox1.Text = a;

    }

    第二种 笨点的方法

    绑定数据

    private void Form1_Load(object sender, EventArgs e) {

    var list = new Dictionary<string, string> { {"this is i1", "001"}, {"this is i2", "002"} };

    comboBox1.Items.AddRange(new object[] {list.ToArray()});

    comboBox1.DisplayMember = "key";

    comboBox1.ValueMember = "value";

    }


    //绑定数据 以上方法失效用这个方法
    Dictionary<string, string> dict = new Dictionary<string, string> {{"baidu.com", "百度"}, {"goolge.com", "谷歌"}, {"qq.com", "腾讯"}};
    comboBox1.DataSource = new BindingSource(dict, null);
    comboBox1.ValueMember = "Key";//文本对应的值
    comboBox1.DisplayMember = "Value";//显示的文本

    获取值

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {

    string a = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;

    textBox1.Text = a;

    }

  • 相关阅读:
    PCL利用RANSAC自行拟合分割平面
    HDU 3062 && HDU 1824 && POJ 3678 && BZOJ 1997 2-SAT
    BZOJ 3670 && BZOJ 3620 && BZOJ 3942 KMP
    BZOJ 1500 Splay 全操作
    Manacher
    POJ 2155 2维线段树 || 2维BIT
    BZOJ 1015 并查集+离线倒序
    NOI Linux JAVA
    UVA 10407 差分思想的运用
    BZOJ 1969 树链剖分+Tarjan缩点
  • 原文地址:https://www.cnblogs.com/simadi/p/6496787.html
Copyright © 2011-2022 走看看