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());
                }
            }

  • 相关阅读:
    win7,win8,win8.1修复IE浏览器的建议
    推荐给.net程序员的学习网站
    OLTP与OLAP
    profiler列名的具体意义
    sp_reset_connection
    IDENTITY
    【读书笔记】Android Handler和Looper流程
    Android视频/音频缓存框架AndroidVideoCache
    Android KeyStore格式转换工具
    使用Android Studio开发NDK
  • 原文地址:https://www.cnblogs.com/greencolor/p/1716281.html
Copyright © 2011-2022 走看看