winform中combox动态添加项

Winformcombobox没有象WebDropDownList那样有个ListItem

但我们可以自己写一个类

    public class ListItem

    {

        private string _displayMember;

        private int _valueMember;

 

        public string DisplayMember

        {

            get { return _displayMember; }

            set { _displayMember = value; }

        }       

 

        public int ValueMember

        {

            get { return _valueMember; }

            set { _valueMember = value; }

        }

 

        public ListItem(string displayMember,int valueMember)

        {

            _displayMember = displayMember;

            _valueMember = valueMember;

        }

 

        ///

        /// 显示值用

        ///

        ///

        public override string ToString()

        {

            return _displayMember;

        }

然后调用combobox.Items.Add()方法,如果是数组就为combox.Items.AddRange()方法

combobox.ValueMember = “ValueMember”;

combobox.DisplayMember = “DisplayMember”;

 

取值的时候直接 (combobox.selectItem as ListItem).ValueMember;

 

你可能感兴趣的:(winform中combox动态添加项)