C#自定义泛型类绑定ComboBox控件

C# WinForm ComboBox 自定义数据项 (ComboBoxItem
)

 

图片 1图片 2

WinForm下的ComboBox默许是以多创作本来设定展现列表的, 这平日不相符大家日常的使用, 

因为大家常见行使一般是键/值对的花样去绑定它的.

那么用键值对的款型如何做?

因为Combox的每二个项的值是三个object, 实际上正是四个键/值对.
自家用的是上面这些类的实例作为它的一个项:

    /// <summary>
    /// ComboBox的项
    /// </summary>
    class ListItem : System.Object
    {
        private string m_sValue = string.Empty;
        private string m_sText = string.Empty;

        /// <summary>
        /// 值
        /// </summary>
        public string Value
        {
            get { return this.m_sValue; }
        }
        /// <summary>
        /// 显示的文件
        /// </summary>
        public string Text
        {
            get { return this.m_sText; }
        }

        public ListItem(string value, string text)
        {
            this.m_sValue = value;
            this.m_sText = text;
        }
        public override string ToString()
        {
            return this.m_sText;
        }
        public override bool Equals(System.Object obj)
        {
            if (this.GetType().Equals(obj.GetType()))
            {
                ListItem that = (ListItem)obj;
                return (this.m_sText.Equals(that.Value));
            }
            return false;
        }
        public override int GetHashCode()
        {
            return this.m_sValue.GetHashCode(); ;
        }

    }

 通过那几个类就足以定义ComboBox的值了, 首先大家定义1个ListItem的清单作为ComboBox的数据源:

            List<ListItem> items = new List<ListItem>();
            items.Add(new ListItem(“0”, “Item_0_Text”));
            items.Add(new ListItem(“1”, “Item_1_Text”));
            items.Add(new ListItem(“2”, “Item_2_Text”));
            items.Add(new ListItem(“3”, “Item_3_Text”));
            items.Add(new ListItem(“4”, “Item_4_Text”));
            items.Add(new ListItem(“5”, “Item_5_Text”));
 

 然后实行对应的设置:

            //将数据源的属性与ComboBox的品质对应
            drpTest.DisplayMember = “Text”;        //显示
            drpTest.ValueMember = “Value”;        //值 

下一场进就能够举行绑定了:

            drpTest.DataSource = items;        //绑定数据 

绑定数据之后, 就能够对其开始展览私下认可选拔项的装置, 取值等操作:

            drpTest.SelectedValue = “4”;        //设定选用项

            //取伏贴前增选的项
            ListItem selectedItem = (ListItem)drpTest.SelectedItem;
            string value = selectedItem.Value;    //值
            string text = selectedItem.Text;    //呈现的文字

 

其余操作大家就像法炮制吧. 呵呵. 

View Code

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class
Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        public struct ComboBoxItem<TKey, TValue>
        {
            private TKey key;
            private TValue value;

            public ComboBoxItem(TKey key, TValue value)
            {
                this.key =
key;
                this.value =
value;
            }

            public TKey Key
            {
                get { return key; }
            }

            public TValue Value
            {
                get { return value; }
            }

            public override string ToString()
            {
                return Value.ToString();
            }
        }

        private void
Form3_Load(object sender, EventArgs e)
        {
            //KeyValuePair<int, string> keys = new
KeyValuePair<int,string>();
            this.comboBox1.Items.Add(new
ComboBoxItem<int,
string>(1,
“Lin”));
        }

        private void
comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item =
(ComboBoxItem<int,
string>)this.comboBox1.SelectedItem;

            Text = item.Value;
        }
    }
}

图片 3)

  

多少个 5月 到1二 月的下拉单

for (int i = 1; i <= 12; i++)
{
    this.comboBox1.Items.Add(
       new ComboBoxItem<int, string>(i,
              String.Concat(i.ToString().PadLeft(2, '0'), "月")));
}

相关文章