在实际工作中,如果能像菜单一样弹出自定义内容,会方便很多,比如查询时,比如下拉列表显示多列信息时,比如在填写某个信息需要查看一些信息树时。这个时候自定义弹出界面就显的非常重要了

我这里其实用到的是网上找到的一个控件(下载地址),控件可以把你装载的任何对象显示出来(这里的对象是指:窗体,自定义控件等),这里文章写出来并不是为了炫耀什么,只是觉得发现些好东西就分享出来而已,同时也做个记录,方便以后查找

开始正文,这里我做一个多列下拉列表来说明:

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);}}

至此 功能实现了,全部代码:

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中

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

比如只显示两列,

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

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

转载于:https://www.cnblogs.com/springSky/archive/2013/02/19/2913993.html

Winform 实现像菜单一样弹出层相关推荐

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

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

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

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

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

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

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

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

  5. layer执行父窗口ajax方法,layer 弹出层 回调函数调用 弹出层页面 函数

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

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

    工作内容: Toolbar(含溢出菜单设置[弹出菜单的使用])的使用 学习分享: Toolbar的使用前提:设置主题 <style name="AppTheme" paren ...

  7. 分享123个JS特效弹出层,总有一款适合您

    分享123个JS特效弹出层,总有一款适合您 123个JS特效弹出层下载链接:https://pan.baidu.com/s/1mH0heedscCrBmft_zOjjwA?pwd=n4eo  提取码: ...

  8. layer弹出层闪退_layer弹出层详解

    1.layer 弹出层iframe页面关闭自身 function closeFrame() { var index = parent.layer.getFrameIndex(window.name); ...

  9. php提交表单关闭弹出层,使用js实现关闭js弹出层的窗口

    本篇文章主要是对使用js实现关闭js弹出层的窗口的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 function toggle() { theObj = document.getE ...

最新文章

  1. python中并发编程基础1
  2. 本地预览图片html和js例子
  3. 2020 ccf推荐中文期刊_CCF推荐国际学术期刊
  4. 带有谓词的Java中的函数样式-第2部分
  5. 实现PHP内部的通知机制,如当一个类的属性发生变化时,另外一个类就可以收到通知...
  6. 海外仓ERP系统功能怎么样?
  7. python安装jupyter出现问题_有关jupyterhub安装的问题
  8. sharepoint 2013/2010/2007 复制工具:SharePoint Content Deployment Wizard
  9. 如何吧本地仓库提交到github_如何将本地代码提交到github远程仓库(第一次,建立连接并且提交)...
  10. Response AddHeader使用实例
  11. HDU 4864 (2014 Multi-University Training Contest 1 )
  12. EDA技术实用教程VHDL篇--Quartus II 13.1实用教程--工程建立
  13. 一招教你如何改变图片的大小?只需三步简单实用
  14. (mac版本)IntelliJ IDEA 常用快捷键
  15. win7计算机图标缩在,win7桌面图标怎么变小?win7桌面图标变小解决方法
  16. 硬盘的读写速度如何计算
  17. spring boot 2.1学习笔记【异常】lombok.javac.apt.LombokProcessor could not be initialized
  18. 透析SpringBoot jar可执行原理
  19. 英特尔® 快速存储技术 RAID 功能;按芯片组/控制器集线器排列
  20. _tmain与main,winMain,wmain收藏

热门文章

  1. portlet_Portlet Servlet JSP
  2. jdbc 数据源_Java数据源,JDBC数据源示例
  3. android 通知栏进度_Android改造下载图片进度通知
  4. android生命周期_Android活动生命周期– 7个阶段和功能
  5. 十佳程序员_面向非技术用户的十佳Web开发工具
  6. eclipse.ini vm参数– eclipse.ini文件位置Mac,Windows
  7. jenkins部署war包到容器(tomcat)
  8. 第二单元总结——多线程设计
  9. mybatis-generator 根据表生成对应文件
  10. ios中获取各种文件的目录路径方法