C# 中combobox 控件初始化

1.从工具箱中找到combobox控件图标拖到winform界面上,IDE会自动生成一个combobox控件,控件名称为comboBox1.

2.在Form1_Load()函数中添加如下代码:

           ArrayList lst = new ArrayList();       //列表
            lst.Add(new Vendor("a_geshi03", "20"));  //在列表中增加对象
            lst.Add(new Vendor("a_geshi04", "30"));

            comboBox1.Items.Clear();                 //清空combobox
            comboBox1.DataSource = lst;           //将lst列表绑定到combobx
            comboBox1.DisplayMember ="Strtemname";// 指定显示的数据项
            comboBox1.ValueMember =  "Strindex";  //指定comboBox1.SelectedValue返回的数据项

3.其中的Vendor可以是自定义的类,如:

  class Vendor
    {
        private string strtemname;
        private string strindex;
        public Vendor(string itemname,string index)
        {
            this.strtemname = itemname;
            this.strindex = index;
        }

        public string Strtemname
        {
            get { return strtemname; }
            set { strtemname = value; }
        }

        public string Strindex
        {
            get { return strindex; }
            set { strindex = value; }
        }
    }

原文地址:https://www.cnblogs.com/sunleinote/p/2220416.html