在實際工作中,如果能像菜單一樣彈出自定義內容,會方便很多,比如查詢時,比如下拉列表顯示多列信息時,比如在填寫某個信息需要查看一些信息樹時。這個時候自定義彈出界面就顯的非常重要了
我這裏其實用到的是網上找到的一個控件(下載地址),控件可以把你裝載的任何對象顯示出來(這裏的對象是指:窗體,自定義控件等),這裏文章寫出來並不是爲了炫耀什麼,只是覺得發現些好東西就分享出來而已,同時也做個記錄,方便以後查找
開始正文,這裏我做一個多列下拉列表來說明:
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 = "四川省樂山市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); } }
至此 功能實現了,全部代碼:
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中
你可以自己解決在彈出層中顯示什麼,
比如只顯示兩列,
還可以設置彈出界面自動適應隨內容的寬度,這樣內容不會被截取顯示,也就是說沒有橫向滾動條出現
以此,我另外做了個下拉列表 大家有興趣的可以下來看看 點擊下載