前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录。

  要求:将弹出框里勾选的内容返回到主面板上。

  工具:委托。

  效果图:(由于是根据项目提取出来的,所以里面的界面有点文字有点奇怪)

    主窗体:

      

    子窗体:(点击浏览之后弹出的对话框)

      

    勾选几项之后,点击确定,主窗体显示:

      

  实现过程:

    这里主要是用到委托实现,所以主要描述一下委托在这里的应用。

    我们要在主窗体(这里的子父窗体都是自己假想)中获取子窗体中的元素,所以首先在主窗体声明一个委托,这个委托是用来干嘛的?我们知道,在子窗体勾选几个选项点击确定之后,在这个事件中,我们需要将勾选的内容传送到主窗体,这里的委托的含义就是:我主窗体有给TextBox显示文本的方法,但是我主窗体干不了这件事儿,因为我没有文本,文本在你子窗体那里,所以主窗体得委托子窗体干一件事儿,这个事儿就是麻烦你子窗体把勾选的东西的文本给我显示到主窗体传的TextBox中。

//1、声明一个委托
public delegate void showText(List<String> ls);

    声明完委托后,在子窗体(Form2/Form3)实例化一个委托,这个委托才是真真正正的委托,是干事的委托。

//2、实例化一个委托
public showText f2;

    那有了委托之后,你子窗体需要干什么事情呢?来,就是干这件事儿:麻烦你帮我把list集合中的字符串显示到textBox1里面去。该方法是在主窗体中写的。

 //3、委托的方法,这个方法应该和第一步是同步实现的,这里暂且记作第3步。   
private void T1Show(List<String> ls)
{
stringList1 = ls;
stringList1.Sort();
this.textBox1.Text = null;
foreach (String item in stringList1)
{
this.textBox1.Text += item + ";";
}
}

    委托子窗体要干的事情有了,接下来就是把这件事委托给子窗体。

//4、把要干的事情委托给子窗体已经创建好的委托载体f2.
objForm.f2 = this.T1Show;

    到这里基本上就实现了子父窗体利用委托进行窗体间通信,先把整个项目的代码展示出来:

主窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; namespace 云状
{
public partial class Form1 : Form
{
//保存当前已经添加到数据库的气象代码
// public List<String> stringList1 = new List<string>();
//private List<String> stringList2 = new List<string>();
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{ Form2 objForm = new Form2();
objForm.FormBorderStyle = FormBorderStyle.None;
//4、把要干的事情委托给子窗体已经创建好的委托载体f2.
objForm.f2 = this.T1Show;
objForm.ShowDialog(); }
//3、委托的方法,这个方法应该和第一步是同步实现的,这里暂且记作第3步。
private void T1Show(List<String> ls)
{
stringList1 = ls;
stringList1.Sort();
this.textBox1.Text = null;
foreach (String item in stringList1)
{
this.textBox1.Text += item + ";";
}
}
private void T2Show(List<String> ls)
{
stringList2 = ls;
stringList2.Sort();
this.textBox2.Text = null;
foreach (String item in stringList2)
{
this.textBox2.Text += item + ";";
}
}
private void button2_Click(object sender, EventArgs e)
{ Form3 objForm = new Form3();
objForm.FormBorderStyle = FormBorderStyle.None;
objForm.f3 = this.T2Show;
objForm.ShowDialog();
} private void button3_Click(object sender, EventArgs e)
{
//入库
}
}
//1、声明一个委托
public delegate void showText(List<String> ls);
}

子窗体Form2代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections; namespace 云状
{
public partial class Form2 : Form
{
//2、实例化一个委托
public showText f2;
public Form2()
{
InitializeComponent();
if (Form1.stringList1 != null)
{
foreach (Control item in this.panel1.Controls)
{
if (item is CheckBox)
{
string str = ((CheckBox)item).Text.Substring(, );
if (Form1.stringList1.Contains(str))
{
((CheckBox)item).Checked = true;
}
}
}
} } private void button1_Click(object sender, EventArgs e)
{
List<String> ls=new List<string>();
foreach(Control item in this.panel1.Controls)
{
if(item is CheckBox)
{
if (((CheckBox)item).Checked==true)
{
ls.Add(((CheckBox)item).Text.Substring(, ));
}
}
}
if(f2!=null)
{
f2(ls);
}
this.Close();
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

子窗体Form3代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 云状
{
public partial class Form3 : Form
{
public showText f3;
public Form3()
{
InitializeComponent();
if (Form1.stringList2 != null)
{
foreach (Control item in this.panel1.Controls)
{
if (item is CheckBox)
{
string str = ((CheckBox)item).Text.Substring(, );
if (Form1.stringList2.Contains(str))
{
((CheckBox)item).Checked = true;
}
}
}
} } private void button1_Click(object sender, EventArgs e)
{
List<String> ls=new List<string>();
foreach (Control item in this.panel1.Controls)
{
if (item is CheckBox)
{
if (((CheckBox)item).Checked == true)
{
ls.Add(((CheckBox)item).Text.Substring(, ));
} }
} if (f3 != null)
{
f3(ls);
} this.Close();
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

  可能会有疑问,不就是传一个List<String>吗,有必要这么麻烦吗?其实这里只是利用委托做事情,委托的其他用处还很广泛,我也在学习之中,以后有什么值得记录的东西,再在这里记录。。。

  

winform利用委托delegate进行窗体间通信的更多相关文章

  1. winform利用委托delegate进行窗体间通信,相同标题已经存在??

    前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...

  2. C# 利用委托事件进行窗体间的传值(新手必看)

    引言: 窗体间传值是每个学习WinForm新手的常见问题,最初级的方法就是 在窗体中先获取到要接受值窗体.然后通过.得到某个空间或者属性,直接赋值,这个需要接收放的窗体属性或者空间必须是public ...

  3. C# 利用委托事件进行窗体间的传值(简化)

    定义委托 public delegate void SendMessageToChildForms(string s); //定义了一个参数是string ,无返回值的委托,名为 SendMessag ...

  4. qt 窗体间通信

    利用qt的信号和槽,可以完成窗体间的通信,下面列出父子窗口利用信号和槽的相关代码. parent窗口: //parent.h #ifndef PARENT_H #define PARENT_H #in ...

  5. C#不同窗体间通信,数据传递

    在一个项目中,很多时候都需要在窗体间进行数据传递和通信,最觉见的是父子窗体之间的数据传递,比如登录ID,各个窗体都需要知道.有很多文章都写了这方面的问题,提出很多优秀的方法,鄙人不才,搜了一些资料之后 ...

  6. Winform利用委托进行窗体间的传值

    在form1.cs中 1.委托的定义 //定义一个委托 public delegate void AddUsrEventHandler(object sender, AddUsrEventHandle ...

  7. winform 利用委托实现窗体传值

    父窗体:Form1    ,有个 textbox1.text ,有个button1 子窗体:Form2  ,有个 textbox1.text ,有个button1 修改Form1 的textbox1. ...

  8. Java:多线程,使用同步锁(Lock)时利用Condition类实现线程间通信

    如果程序不使用synchronized关键字来保证同步,而是直接使用Lock对象来保证同步,则系统中不存在隐式的同步监视器,也就不能用wait().notify().notifyAll()方法进行线程 ...

  9. WPF:事件委托对于不同界面间通信的应用

    界面1内设定点击事件,生成Path用事件传出public partial class TemplateWindow : Window     {         internal delegate v ...

随机推荐

  1. mac下使用命令行打包出现bash gradle command not found的解决方案

    命令行打包的时候出现 bash gradle command not found这个问题,主要是因为gradle环境丢失.需要重新配置gradle的环境变量. 1. gradle路径的查找 然后gra ...

  2. [转]JAVA的动态代理机制及Spring的实现方式

    JAVA 代理实现 代理的实现分动态代理和静态代理,静态代理的实现是对已经生成了的JAVA类进行封装. 动态代理则是在运行时生成了相关代理累,在JAVA中生成动态代理一般有两种方式. JDK自带实现方 ...

  3. TextView 实现跑马灯效果

    在String.xml中添加: <string name="txt">跑马灯效果,我跑啊跑</string>在layout/mian.xml中添加TextV ...

  4. nested exception is java.sql.SQLException: Cannot convert value '0000-00-00 00:00:00' from column 14 to TIMESTAMP.

    无法将"0000-00-00 00:00:00"转换为TIMESTAMP 2017-05-08 00:56:59 [ERROR] - cn.kee.core.dao.impl.Ge ...

  5. JAVA POI 应用系列(2)--读取Excel

    添加maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  6. Akka(5): ConsistentHashing Router - 可选定Routee的任务分配模式

    上一篇讨论里我们介绍了几种任务分配(Routing)模式.Akka提供的几种现成智能化Routing模式大多数是通过对用户屏蔽具体的运算Routee选择方式来简化Router使用,提高智能程度,所以我 ...

  7. 在chrome下鼠标拖动层变文本形状的问题

    学JQ也有一段时间了,想自己写个鼠标拖动层移动的效果(很简单,只是为了练习而已)于是就写下了下面的代码 <!DOCTYPE html> <html> <head> ...

  8. 关于Oracle、SqlServer 的sql递归查询

    递归查询所有子节点 建人员表  hrmresource 主键     姓名   上级ID 层级关系:- 4      - 3           - 2                - 1      ...

  9. Discuz添加自定义模板广告

    在做Discuz中广告的时候碰到个大问题,现在我需要做一个轮播的通屏广告位,调用广告图片的代码应该是以下代码:<ul>     <li style="background: ...

  10. 记一次使用搬瓦工VPS的经历

    自己因为有需求上Google,以前是通过修改hosts的方法实现访问Google,但是最近不知道为什么改hosts后还是无法访问Google,于是决定搭建VPS来实现科学上网,看了一下价格,作为穷逼学 ...