泛型的小例子,C#3.5版

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

namespace Comb
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }
        
private void addSt()
        {
            List
<ComboBoxItem<Student>> list = new List<ComboBoxItem<Student>>();
            list.Add(
new ComboBoxItem<Student>(""null));
            Student s1 
= new Student("张三"15);
            Student s2 
= new Student("李四"20);
            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)
        {
            
//addSt();
            if (comboBox1.SelectedIndex > 0)
            {
                Student s 
= (Student)comboBox1.SelectedValue;
                MessageBox.Show(s.Age.ToString());
            }
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            addSt();
        }

    }

    
public class ComboBoxItem<T>
    {
        
//ItemText显示的文字
       
// private string _itemText;
        public string ItemText
        {
            
get;set;
            
//get { return _itemText ; }
            
//set { _itemText = value; }
        }
        
//ItemValue实际的值
       
// private T _itemValue;
        public T ItemValue
        {
            
get;set;
            
//get { return _itemValue; }
            
//set { _itemValue = value; }
        }
        
//构造方法
        public ComboBoxItem(string itemText, T itemValue)
        {
            
//this._itemText = itemText;
            
//this._itemValue = itemValue;
            ItemText = itemText;
            ItemValue 
= itemValue;
        }
        
//自定义方法
        
//确定指定的对象是否等于当前对象
        public override bool Equals(object obj)
        {
           
// 2643993
            if (obj is ComboBoxItem<T>)
            {
                ComboBoxItem
<T> rhs = (ComboBoxItem<T>)obj;
               
// if (_itemText.Equals(rhs.ItemText) && _itemValue.Equals(rhs.ItemValue))
                if(ItemText.Equals(rhs.ItemText)&&ItemValue.Equals(rhs.ItemValue))
                    
return true;
                
else
                    
return false;
            }
            
else
                
return false;
        }
        
//获取当前对象的哈希代码
        public override int GetHashCode()
        {
            
return ItemText.GetHashCode() + ItemValue.GetHashCode();
        }
        
//重载相等操作符
        public static bool operator ==(ComboBoxItem<T> lhs, ComboBoxItem<T> rhs)
        { 
return lhs.Equals(rhs); }
        
//重载不等
        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;
        }
    }
}
posted on 2008-05-13 11:40  雲淡風清  阅读(477)  评论(1编辑  收藏  举报