看一教程的ComboBox定制,看不懂下面的代码,谁能帮忙解释下,非常感谢^_^主要目的是为了实现动态填充ComboBox1、先是新建了一个Product类,主要看不懂的地方在代码中有标注:namespace MyComboBox
{
public class Product
{
public Product(string code, string name)
{
this._code = code;
this._name = name;
} private string _code;
public string Code
{
get { return _code; }
set { _code = value; }
} private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
} public override string ToString()
{
return _code + " " + _name;
}
//这个方法看不太懂,怎么一会IList,一会List,这里是泛型方法么?希望能得到您的详解,谢谢!
public static IList<Product> GetAllProducts()
{
IList<Product> all=new List<Product>();
all.Add(new Product ("01", "浙江"));
all.Add(new Product ("02","杭州"));
all.Add(new Product ("03","千岛湖"));
return all;
}
}
}
2、通过点击按钮实现动态填充:private void button1_Click(object sender, EventArgs e)
{
IList<Product> all = Product.GetAllProducts(); foreach (Product prod in all)
{
this.comboBox1.Items.Add(prod);
}
}
3、选中某一项时弹出消息显示选择的Product的Name: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Product selectedProduct = (Product)this.comboBox1.SelectedItem;
MessageBox.Show(selectedProduct.Name);
}
谢谢您看完帖子。
{
public class Product
{
public Product(string code, string name)
{
this._code = code;
this._name = name;
} private string _code;
public string Code
{
get { return _code; }
set { _code = value; }
} private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
} public override string ToString()
{
return _code + " " + _name;
}
//这个方法看不太懂,怎么一会IList,一会List,这里是泛型方法么?希望能得到您的详解,谢谢!
public static IList<Product> GetAllProducts()
{
IList<Product> all=new List<Product>();
all.Add(new Product ("01", "浙江"));
all.Add(new Product ("02","杭州"));
all.Add(new Product ("03","千岛湖"));
return all;
}
}
}
2、通过点击按钮实现动态填充:private void button1_Click(object sender, EventArgs e)
{
IList<Product> all = Product.GetAllProducts(); foreach (Product prod in all)
{
this.comboBox1.Items.Add(prod);
}
}
3、选中某一项时弹出消息显示选择的Product的Name: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Product selectedProduct = (Product)this.comboBox1.SelectedItem;
MessageBox.Show(selectedProduct.Name);
}
谢谢您看完帖子。
解决方案 »
- C# Linq查询的问题
- 紧急!!!!(如何根据类名作为参数,动态调用指定类的方法)
- 在 sqlserver查询分析器中 用哪条命令查看一个表的结构 或者索引等其它对象
- 为什么不能连接win2003系统下的数据库
- 主键为字符时GridView更新数据时:能找到带参数的非泛型方法
- 关于System.Drawing.Graphics类的图片截取
- 在wince下socket 通信传输速率上限的问题~
- 经过包装的COM对象(COM Interop)能否存入数据库的字段中
- Internet Explorer 中的控件
- 小第求救如何将颜色存进数据库并读取到颜色对象 100分
- WinForms程序在其它机器上不能运行的问题
- C#如何向系统发送模拟移动的消息?
例如
Interface IShape
{
void Draw();
}class Circle : IShape
{
void Draw()
{
//Draw的实现
}
}你可以将Circle的实例付给IShape类型的引用
IShape shape = new Circle();
在public static IList<Product> GetAllProducts()
中接口作为一个返回值,返回的其实就是list集合
实际上你的IList<Product>使用的generic class是IList<T>
它应该类似于下面的样子
public interface IList<T> : ICollection<T>,
IEnumerable<T>, IEnumerable
{
void Add(T item); //因为是interface因此只有声明,没有实现。
...其它接口
}而List<Product> 使用的是List<T>
它看起来像这个样子:
public class List<T> : IList<T>, ICollection<T>,
IEnumerable<T>, IList, ICollection, IEnumerable
{
void Add(T item)
{
//实现
}
}需要注意的是,你用一种类型实例化List<T>,只能付给使用这种类型的引用的接口IList<T>
例如
IList<int> a = new List<int>() //corrent
IList<int> b = new List<double>() //compile error.good luck
它应该类似于下面的样子
public interface IList <T> : ICollection <T>,
IEnumerable <T>, IEnumerable
{
void Add(T item); //因为是interface因此只有声明,没有实现。
...其它接口
} 而List <Product> 使用的是List <T>
它看起来像这个样子:
public class List <T> : IList <T>, ICollection <T>,
IEnumerable <T>, IList, ICollection, IEnumerable
{
void Add(T item)
{
//实现
}
} 需要注意的是,你用一种类型实例化List <T>,只能付给使用这种类型的引用的接口IList <T>
例如
IList <int> a = new List <int>() //corrent
IList <int> b = new List <double>() //compile error.
List是泛型。
List是泛型类。
public static IList<Product> GetAllProducts()
{
IList<Product> all=new List<Product>();
all.Add(new Product ("01", "浙江"));
all.Add(new Product ("02","杭州"));
all.Add(new Product ("03","千岛湖"));
return all;
}
应该是一个泛型实体吧
all.Add(new Product ("01", "浙江")); Add方法就是往实例里加东西