原文: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 = , SName = "张阳", SAge = , SCode = "zy", SAddress = "广东省中山市9999999" });
stList.Add(new Student() { SId = , SName = "欧阳新文", SAge = , SCode = "oyxw", SAddress = "广东省广州市99" });
stList.Add(new Student() { SId = , SName = "宇文化及", SAge = , SCode = "ywhj", SAddress = "广东省湛江市2222" });
stList.Add(new Student() { SId = , SName = "温斯特梅拉", SAge = , SCode = "wstml", SAddress = "广东省茂名市" });
stList.Add(new Student() { SId = , SName = "唐兵", SAge = , SCode = "tb", SAddress = "四川省阆中市555555555555" });
stList.Add(new Student() { SId = , SName = "杨红军", SAge = , SCode = "yhj", SAddress = "四川省乐山市22222222222222222222" });
stList.Add(new Student() { SId = , SName = "张翠山", SAge = , SCode = "zcs", SAddress = "四川省南充市7777777777777777" });
stList.Add(new Student() { SId = , SName = "张三丰", SAge = , SCode = "zsf", SAddress = "广东省中山市555" });
stList.Add(new Student() { SId = , SName = "何月", SAge = , SCode = "hy", SAddress = "广东省中山市88888" });
stList.Add(new Student() { SId = , SName = "何宝生", SAge = , SCode = "hbs", SAddress = "广东省中山市77" });
stList.Add(new Student() { SId = , SName = "王家新", SAge = , SCode = "wjx", SAddress = "广东省茂名市8" });
stList.Add(new Student() { SId = , SName = "肖月伦", SAge = , SCode = "xyl", SAddress = "广东省茂名市7777777" });
stList.Add(new Student() { SId = , SName = "王岳伦", SAge = , SCode = "wyl", SAddress = "广东省中山市888888888888" });
stList.Add(new Student() { SId = , SName = "紫阳红玉", SAge = , SCode = "zyhy", SAddress = "四川省南充市2" });
stList.Add(new Student() { SId = , SName = "雷小兵", SAge = , SCode = "lxb", SAddress = "广东省中山市999999999999999" });
stList.Add(new Student() { SId = , SName = "张郭老", SAge = , SCode = "zgl", SAddress = "四川省南充市333333333333333333333333" });
stList.Add(new Student() { SId = , SName = "汤小雨", SAge = , SCode = "txy", SAddress = "四川省乐山市333333333333" });
stList.Add(new Student() { SId = , SName = "吴志胜", SAge = , SCode = "wzs", SAddress = "四川省乐山市33" });
stList.Add(new Student() { SId = , SName = "伍国天", SAge = , SCode = "wgt", SAddress = "四川省阆中市6666" });
stList.Add(new Student() { SId = , SName = "朱新朝", SAge = , 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 > && dgvStudentList.SelectedRows.Count > )
{
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 = , SName = "张阳", SAge = , SCode = "zy", SAddress = "广东省中山市9999999" });
stList.Add(new Student() { SId = , SName = "欧阳新文", SAge = , SCode = "oyxw", SAddress = "广东省广州市99" });
stList.Add(new Student() { SId = , SName = "宇文化及", SAge = , SCode = "ywhj", SAddress = "广东省湛江市2222" });
stList.Add(new Student() { SId = , SName = "温斯特梅拉", SAge = , SCode = "wstml", SAddress = "广东省茂名市" });
stList.Add(new Student() { SId = , SName = "唐兵", SAge = , SCode = "tb", SAddress = "四川省阆中市555555555555" });
stList.Add(new Student() { SId = , SName = "杨红军", SAge = , SCode = "yhj", SAddress = "四川省乐山市22222222222222222222" });
stList.Add(new Student() { SId = , SName = "张翠山", SAge = , SCode = "zcs", SAddress = "四川省南充市7777777777777777" });
stList.Add(new Student() { SId = , SName = "张三丰", SAge = , SCode = "zsf", SAddress = "广东省中山市555" });
stList.Add(new Student() { SId = , SName = "何月", SAge = , SCode = "hy", SAddress = "广东省中山市88888" });
stList.Add(new Student() { SId = , SName = "何宝生", SAge = , SCode = "hbs", SAddress = "广东省中山市77" });
stList.Add(new Student() { SId = , SName = "王家新", SAge = , SCode = "wjx", SAddress = "广东省茂名市8" });
stList.Add(new Student() { SId = , SName = "肖月伦", SAge = , SCode = "xyl", SAddress = "广东省茂名市7777777" });
stList.Add(new Student() { SId = , SName = "王岳伦", SAge = , SCode = "wyl", SAddress = "广东省中山市888888888888" });
stList.Add(new Student() { SId = , SName = "紫阳红玉", SAge = , SCode = "zyhy", SAddress = "四川省南充市2" });
stList.Add(new Student() { SId = , SName = "雷小兵", SAge = , SCode = "lxb", SAddress = "广东省中山市999999999999999" });
stList.Add(new Student() { SId = , SName = "张郭老", SAge = , SCode = "zgl", SAddress = "四川省南充市333333333333333333333333" });
stList.Add(new Student() { SId = , SName = "汤小雨", SAge = , SCode = "txy", SAddress = "四川省乐山市333333333333" });
stList.Add(new Student() { SId = , SName = "吴志胜", SAge = , SCode = "wzs", SAddress = "四川省乐山市33" });
stList.Add(new Student() { SId = , SName = "伍国天", SAge = , SCode = "wgt", SAddress = "四川省阆中市6666" });
stList.Add(new Student() { SId = , SName = "朱新朝", SAge = , 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 > && dgvStudentList.SelectedRows.Count > )
{
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中

你可以自己解决在弹出层中显示什么,

比如只显示两列,

还可以设置弹出界面自动适应随内容的宽度,这样内容不会被截取显示,也就是说没有横向滚动条出现

以此,我另外做了个下拉列表 大家有兴趣的可以下来看看  点击下载

Winform 实现像菜单一样弹出层的更多相关文章

  1. 【position也可以很复杂】当弹出层遇上了鼠标定位(下)

    前言 接着昨天的内容写,为了保证内容连续性,这里还是把昨天的内容拷了过来. 请用现代浏览器测试 引出问题 有图有真相,我们来看一个智联招聘里面经常出现的图层: 他这个是没有什么问题的,我们来简单看看其 ...

  2. 【position也可以很复杂】当弹出层遇上了鼠标定位(上)

    前言 周五时同事有一个关于弹出层的问题没有解决,但是面临下班问题,我有点不舒服,便叫回去周六过来解决,但是上周六病了,所以请了个假,于是故事发生啦.... 今天上班时候,组员们卡到了那个地方,然后结果 ...

  3. layer 弹出层 回调函数调用 弹出层页面 函数

    1.项目中用到layer 弹出层,定义一个公用的窗口,问题来了窗口弹出来了,如何保存页面上的数据呢?疯狂百度之后,有了结果,赶紧记下. 2.自己定义的公共页面方法: layuiWindow: func ...

  4. webui-popover 一个轻量级的jquery弹出层插件

    该提示框插件可以和Bootstrap完美结合,但是并不一定需要和Bootstrap一起使用.它支持IE7以上的浏览器. 首先要引入需要的css  js  文件 <link rel="s ...

  5. 分享一个JQuery弹出层插件

    JQuery插件TipsWindown 1.1 一个基于jQuery的弹出层.支持拖拽,支持内容为文字,图片,URL等!至于兼容性.在IE6下,弹出对像无法绝对固定.其他应该没啥大问题: 最新更新:( ...

  6. adminlte+layui框架搭建3 - layui弹出层

    在amdinlte首页引入layui.js 和layui.css后添加代码 <script> layui.use(['layer'], function () { var layer = ...

  7. JS弹出层制作,以及移动端禁止弹出层下内容滚动,overflow:hidden移动端失效问题

    HTML <div class="layer"> <div class="menu-list"> <span>社会</ ...

  8. Android的Toolbar(含溢出菜单设置[弹出菜单的使用])的使用PopMenu的样式

    http://blog.csdn.net/yingtian648/article/details/52432438(转载) 1.在Toolbar.xml中设置弹出菜单的风格(app:popupThem ...

  9. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

随机推荐

  1. Thread.join()分析方法

    API: join public final void join() throws InterruptedException 等待该线程终止. 抛出: InterruptedException - 假 ...

  2. hdu 1429 胜利大逃亡(延续)(BFS+比特压缩)

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  3. 【Linux探索之旅】第一部分第四课:磁盘分区,并完成Ubuntu安装

    内容简介 1.第一部分第四课:磁盘分区,并完成Ubuntu安装 2.第一部分第五课预告:Unity桌面,人生若只如初见 磁盘分区 上一课我们正式开始安装Ubuntu了,但是到了分区的那一步,小编却戛然 ...

  4. client多线程

    1.多线程对象 对象可以是多线程访问,线程可以在这里分为两类: 为完成内部业务逻辑的创建Thread对象,线程需要访问对象. 使用对象的线程外部对象. 进一步假设更精细的划分.业主外螺纹成线等线,. ...

  5. Swift学习资料

    http://www.hangge.com/blog/cache/category_72_1.html

  6. 每天进步一点点——Linux文件锁编程flock

    转载请注明出处:http://blog.csdn.net/cywosp/article/details/30083015 1. 场景概述     在多线程开发中.相互排斥锁能够用于对临界资源的保护,防 ...

  7. Oracle 重建索引脚本

    该指数是一个有力的武器,以提高数据库的查询性能. 没有索引,喜欢同样的标签库没有书籍,找书,他们想预订比登天还难.中,尤其是在批量的DML的情形下会产生对应的碎片.以及B树高度会发生对应变化.因此能够 ...

  8. LeetCode——ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. hdoj1010Starship Troopers (树dp,依赖背包)

    称号:hdoj1010Starship Troopers 题意:有一个军队n个人要占据m个城市,每一个城市有cap的驻扎兵力和val的珠宝,并且这m个城市的占率先后具有依赖关系,军队的每一个人能够打败 ...

  10. Android CTS測试Fail项改动总结(四)

    Android5.1上的測试 1.android.security.cts.SELinuxDomainTest# testInitDomain fail 打印的log junit.framework. ...