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

  • 相关阅读:
    shell之ping减少时间间隔&ping的次数&用IP1去ping IP2的技巧
    kali界面乱码解决方案记录
    win10子系统kali-linux安装图形化界面总结
    树莓派4 64bit 编译安装QT5.13.2 和 Redis Desktop Manager 2020.1-dev
    树莓派4 (8GB) RaspiOS 64 bit 入手配置流程 2020-06-10
    阿里巴巴Java开发手册(泰山版)个人阅读精简
    Java 8 新API Steam 流 学习笔记
    IDEA中maven项目部署到云服务器上(简易)
    收藏模块的设计
    js常用代码片段(更新中)
  • 原文地址:https://www.cnblogs.com/greencolor/p/1716281.html
Copyright © 2011-2022 走看看