在实际工作中,如果能像菜单一样弹出自定义内容,会方便很多,比如查询时,比如下拉列表显示多列信息时,比如在填写某个信息需要查看一些信息树时。这个时候自定义弹出界面就显的非常重要了
我这里其实用到的是网上找到的一个控件(下载地址),控件可以把你装载的任何对象显示出来(这里的对象是指:窗体,自定义控件等),这里文章写出来并不是为了炫耀什么,只是觉得发现些好东西就分享出来而已,同时也做个记录,方便以后查找
开始正文,这里我做一个多列下拉列表来说明:
1、新建winform项目:PopupApplication
2、添加引用,引用上面下载的dll文件
3、因为要显示数据,所以这里需要构造一个数据源,因此我建了一个对象Student,属性:SId,SCode,SName,SAge,SAddress
namespace PopupApplication
{
public class Student
{
public int SId { get; set; }
public string SCode { get; set; }
public string SName { get; set; }
public int SAge { get; set; }
public string SAddress{get;set;}
}
}
4、创建用户控件:StudentListControl
5、在用户控件中添加一个DataGridView命名:dgvStudentList 和TextBox命名:txtKeys,DataGridView是用来显示数据列表的,TextBox是用来让用户输入关键字用来检索信息用的
如图:
6、构建数据源并绑定数据,代码:
private List<Student> _dataList = new List<Student>();
private TextBox _txtBox;
public StudentListControl(TextBox txtBox)
{
InitializeComponent();
_txtBox = txtBox;
_dataList = GetDataList();
}
private void StudentListControl_Load(object sender, EventArgs e)
{
dgvStudentList.DataSource = _dataList;
}
/// <summary>
/// 构造数据源
/// </summary>
/// <returns></returns>
private List<Student> GetDataList()
{
List<Student> stList = new List<Student>();
stList.Add(new Student() { SId = 1, SName = "张阳", SAge = 15, SCode = "zy", SAddress = "广东省中山市9999999" });
stList.Add(new Student() { SId = 2, SName = "欧阳新文", SAge = 17, SCode = "oyxw", SAddress = "广东省广州市99" });
stList.Add(new Student() { SId = 3, SName = "宇文化及", SAge = 18, SCode = "ywhj", SAddress = "广东省湛江市2222" });
stList.Add(new Student() { SId = 4, SName = "温斯特梅拉", SAge = 19, SCode = "wstml", SAddress = "广东省茂名市" });
stList.Add(new Student() { SId = 6, SName = "唐兵", SAge = 24, SCode = "tb", SAddress = "四川省阆中市555555555555" });
stList.Add(new Student() { SId = 8, SName = "杨红军", SAge = 24, SCode = "yhj", SAddress = "四川省乐山市22222222222222222222" });
stList.Add(new Student() { SId = 11, SName = "张翠山", SAge = 23, SCode = "zcs", SAddress = "四川省南充市7777777777777777" });
stList.Add(new Student() { SId = 12, SName = "张三丰", SAge = 27, SCode = "zsf", SAddress = "广东省中山市555" });
stList.Add(new Student() { SId = 13, SName = "何月", SAge = 28, SCode = "hy", SAddress = "广东省中山市88888" });
stList.Add(new Student() { SId = 14, SName = "何宝生", SAge = 32, SCode = "hbs", SAddress = "广东省中山市77" });
stList.Add(new Student() { SId = 15, SName = "王家新", SAge = 33, SCode = "wjx", SAddress = "广东省茂名市8" });
stList.Add(new Student() { SId = 23, SName = "肖月伦", SAge = 34, SCode = "xyl", SAddress = "广东省茂名市7777777" });
stList.Add(new Student() { SId = 22, SName = "王岳伦", SAge = 25, SCode = "wyl", SAddress = "广东省中山市888888888888" });
stList.Add(new Student() { SId = 24, SName = "紫阳红玉", SAge = 45, SCode = "zyhy", SAddress = "四川省南充市2" });
stList.Add(new Student() { SId = 27, SName = "雷小兵", SAge = 30, SCode = "lxb", SAddress = "广东省中山市999999999999999" });
stList.Add(new Student() { SId = 28, SName = "张郭老", SAge = 18, SCode = "zgl", SAddress = "四川省南充市333333333333333333333333" });
stList.Add(new Student() { SId = 30, SName = "汤小雨", SAge = 24, SCode = "txy", SAddress = "