Winform实现像菜单⼀样弹出层
原⽂:
在实际⼯作中,如果能像菜单⼀样弹出⾃定义内容,会⽅便很多,⽐如查询时,⽐如下拉列表显⽰多列信息时,⽐如在填写某个信息需要查看⼀些信息树时。这个时候⾃定义弹出界⾯就显的⾮常重要了
我这⾥其实⽤到的是⽹上到的⼀个控件(),控件可以把你装载的任何对象显⽰出来(这⾥的对象是指:窗体,⾃定义控件等),这⾥⽂章写出来并不是为了炫耀什么,只是觉得发现些好东西就分享出来⽽已,同时也做个记录,⽅便以后查
开始正⽂,这⾥我做⼀个多列下拉列表来说明:
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 = "⼴东省茂名市" });
datagridview数据源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 = "四川省乐⼭市333333333333" });
stList.Add(new Student() { SId = 31, SName = "吴志胜", SAge = 26, SCode = "wzs", SAddress = "四川省乐⼭市33" });
stList.Add(new Student() { SId = 32, SName = "伍国天", SAge = 32, SCode = "wgt", SAddress = "四川省阆中市6666" });
stList.Add(new Student() { SId = 33, SName = "朱新朝", SAge = 33, SCode = "zxz", SAddress = "四川省阆中市666666666666666" });
return stList;
}
7、给txtKeys控件添加TextChange事件:
private void txtKeys_TextChanged(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtKeys.Text))
{
return;
}
var resultList = _dataList.FindAll(std=>std.SName.Contains(txtKeys.Text) || std.SAddress.Contains(txtKeys.Text));
dgvStudentList.DataSource = resultList;
}
8、给dgvStudentList添加点击事件,当点击列表的时候需要把选中的值显⽰到需要显⽰选中值的TextBox中
private void dgvStudentList_Click(object sender, EventArgs e)
{
if (dgvStudentList.RowCount > 0 && dgvStudentList.SelectedRows.Count > 0)
{
DataGridViewCell nameCell= dgvStudentList.CurrentRow.Cells["ColumnSName"];
DataGridViewCell addressCell = dgvStudentList.CurrentRow.Cells["ColumnSAddress"];
string strName = nameCell != null && nameCell.Value != null ? nameCell.Value.ToString() : "";
string strAddress = addressCell != null && addressCell.Value != null ? addressCell.Value.ToString() : "";
_txtBox.Text = string.Format("{0},{1}",strName,strAddress);
}
}
9、在Form1界⾯添加TextBox控件命名:txtSelectValue,添加如下代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txtSelectValue_Click(object sender, EventArgs e)
{
StudentListControl uc = new StudentListControl(txtSelectValue);
Popup pop = new Popup(uc);
pop.Show(txtSelectValue, false);
}
}
⾄此功能实现了,全部代码:
View Code
using System;
using System.Windows.Forms;
using PopupTool;
namespace PopupApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void txtSelectValue_Click(object sender, EventArgs e)
{
StudentListControl uc = new StudentListControl(txtSelectValue);
Popup pop = new Popup(uc);
pop.Show(txtSelectValue, false);
}
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace PopupApplication
{
public partial class StudentListControl : UserControl
{
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 = "四川省乐⼭市333333333333" });
stList.Add(new Student() { SId = 31, SName = "吴志胜", SAge = 26, SCode = "wzs", SAddress = "四川
省乐⼭市33" });
stList.Add(new Student() { SId = 32, SName = "伍国天", SAge = 32, SCode = "wgt", SAddress = "四川省阆中市6666" });
stList.Add(new Student() { SId = 33, SName = "朱新朝", SAge = 33, SCode = "zxz", SAddress = "四川省阆中市666666666666666" });
return stList;
}
private void txtKeys_TextChanged(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtKeys.Text))
{
return;
}
var resultList = _dataList.FindAll(std=>std.SName.Contains(txtKeys.Text) || std.SAddress.Contains(txtKeys.Text));
dgvStudentList.DataSource = resultList;
}
private void dgvStudentList_Click(object sender, EventArgs e)
{
if (dgvStudentList.RowCount > 0 && dgvStudentList.SelectedRows.Count > 0)
{
DataGridViewCell nameCell= dgvStudentList.CurrentRow.Cells["ColumnSName"];
DataGridViewCell addressCell = dgvStudentList.CurrentRow.Cells["ColumnSAddress"];
string strName = nameCell != null && nameCell.Value != null ? nameCell.Value.ToString() : "";
string strAddress = addressCell != null && addressCell.Value != null ? addressCell.Value.ToString() : "";
_txtBox.Text = string.Format("{0},{1}",strName,strAddress);
this.Dispose();
}
}
}
}
效果图:点击Textbox后弹出⾃定义控件
在弹出控件中输⼊关键字:温
选中⼀条记录后弹出界⾯消失,并把选中的值显⽰在TextBox中
你可以⾃⼰解决在弹出层中显⽰什么,
⽐如只显⽰两列,
还可以设置弹出界⾯⾃动适应随内容的宽度,这样内容不会被截取显⽰,也就是说没有横向滚动条出现以此,我另外做了个下拉列表⼤家有兴趣的可以下来看看
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论