zoukankan      html  css  js  c++  java
  • 泛型 类 ComboBoxItem<T>

    Convert ComboBoxItem into  Class array, which contains different class type

        public class ComboBoxItem<T>
        {
          
            private string _itemText;
            public string ItemText
            {
                get { return _itemText; }
                set { _itemText = value; }
            }
          

          
            private T _itemValue;
            public T ItemValue
            {
                get { return _itemValue; }
                set { _itemValue = value; }
            }
          

          
            public ComboBoxItem(string itemText, T itemValue)
            {
                this._itemText = itemText;
                this._itemValue = itemValue;
            }
          
            /**/
            /// <summary>
            /// 确定指定的对象是否等于当前对象。
            /// </summary>
            public override bool Equals(object obj)
            {
                if (obj is ComboBoxItem<T>)
                {
                    ComboBoxItem<T> rhs = (ComboBoxItem<T>)obj;
                    if (this._itemText.Equals(rhs.ItemText) && this._itemValue.Equals(rhs.ItemValue))
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            /**/
            /// <summary>
            /// 获取当前对象的哈希代码。
            /// </summary>
            public override int GetHashCode()
            {
                return ItemText.GetHashCode() + ItemValue.GetHashCode();
            }

            /**/
            /// <summary>
            /// 重载相等操作符
            /// </summary>
            public static bool operator ==(ComboBoxItem<T> lhs, ComboBoxItem<T> rhs)
            {
                return lhs.Equals(rhs);
            }
            /**/
            /// <summary>
            /// 重载不等操作符
            /// </summary>
            public static bool operator !=(ComboBoxItem<T> lhs, ComboBoxItem<T> rhs)
            {
                return !(lhs.Equals(rhs));
            }
          
        }

      public class Student
        {
         
            private string _name;
            public string Name
            {
                get { return _name; }
            }
         
            private int _age;
            public int Age
            {
                get { return _age; }
            }
         
            public Student(string name,int age)
            {
                _name = name;
                _age = age;
            }

        }

        public Form1()
            {
                InitializeComponent();
                List<ComboBoxItem<Student>> list = new List<ComboBoxItem<Student>>();
                list.Add(new ComboBoxItem<Student>("", null));
                Student s1 = new Student("Name 1", 15);
                Student s2 = new Student("Name 2", 16);
                list.Add(new ComboBoxItem<Student>(s1.Name, s1));
                list.Add(new ComboBoxItem<Student>(s2.Name, s2));
                //绑定到下拉框
                comboBox1.DataSource = list;
                comboBox1.DisplayMember = "ItemText";//显示的文字
                comboBox1.ValueMember = "ItemValue";//背后的对象
            }

          

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.SelectedIndex > 0)
                {
                   
                    Student s = (Student)comboBox1.SelectedValue;//获取Student对象
                   
                    MessageBox.Show(s.Age.ToString());
                }
            }

  • 相关阅读:
    rpm包的管理
    多态应用实例--制作饮品
    纯虚函数和抽象类
    rpm包的管理
    动态监控进程
    封装继承和多态
    计网面试题
    模拟浏览器加载页面时的版本下载
    xpath中extract()使用
    MVC+EF CODE FIRST的使用
  • 原文地址:https://www.cnblogs.com/greencolor/p/1716281.html
Copyright © 2011-2022 走看看