copy自:https://www.cnblogs.com/hugoNB/p/7130562.html,这个作者写的浅显易懂,就复制下来自己看

实现过程:

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

    我们要在主窗体(这里的子父窗体都是自己假想)中获取子窗体中的元素,所以首先在主窗体声明一个委托,这个委托是用来干嘛的?我们知道,在子窗体勾选几个选项点击确定之后,在这个事件中,我们需要将勾选的内容传送到主窗体,这里的委托的含义就是:我主窗体有给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(0, 2);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(0, 2));}}}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(0, 2);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(0, 2));}}}if (f3 != null){f3(ls);}this.Close();}private void button2_Click(object sender, EventArgs e){this.Close();}}
}

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

转载于:https://www.cnblogs.com/DoudouZhang/p/9804252.html

利用委托 实现窗体间通信,非原创相关推荐

  1. C#利用委托实现窗体间的值传递

    C#利用委托实现窗体间的值传递 A.网上有很多方法,大家可搜一下,都可用. B.这里只是说明下是只利用委托,学习基本的委托运用. 方法如下: 1.C#建立一个默认工程,默认窗体Form1 2.加入一个 ...

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

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

  3. arouter跨module传递消息_利用ARouter实现组件间通信,解决子模块调用主模块问题...

    一年之前我写过一篇组件间通信的博客Android模块间通信(不使用三方库),当时用的是反射,自己去维护一套"对应关系"(分别给每个模块命名,分别给每个类命名帮助反射找到对应的类), ...

  4. WinForm(十五)窗体间通信

    在很多WinForm的程序中,会有客户端之间相互通信的需求,或服务端与客户端通信的需求,这时就要用到TCP/IP的功能.在.NET中,主要是通过Socket来完成的,下面的例子是通过一个TcpList ...

  5. 利用事件进行窗体间传值

    为什么80%的码农都做不了架构师?>>>    举例,现在有两个窗体,SelectForm窗体查询信息集合,使用DataGridview显示信息集合,EditForm窗体添加和修改信 ...

  6. 用委托实现窗体间传值

    1.新建一个工程.在Form1中添加一个Label和一个Button.新建一个事件类,让它有一个string 类型的属性,用于传值. 1 ///ReturnValueEventArgs.cs  2 u ...

  7. 1030利用三层交换机实现VLAN间通信

    实验相关文件在附件中 转载于:https://blog.51cto.com/network0546/219543

  8. C# 利用委托事件进行窗体间的传值

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

  9. 系统间通信1:阻塞与非阻塞式通信A

    版权声明:本文引用https://yinwj.blog.csdn.net/article/details/48274255 从这篇博文开始,我们将进入一个新文章系列.这个文章系列专门整理总结了目前系统 ...

最新文章

  1. 专业的秘密 | 南方医科大学生物信息学专业
  2. 1.1 基本图像导入、处理和导出
  3. 语言速算24点的小窍门_期末备考:小学数学期末常考题型汇总+速算解题思路分析...
  4. 关于他们回答的 怎样在桌面建一个python GUI的快捷方式 这个问题
  5. 等待十年,史上第一个 64 位版 Visual Studio 将于今夏公开首个预览版!
  6. 区块链跟银行有什么关系?
  7. simulink中文_CarSimamp;Simulink 联合仿真案例
  8. isnull pivot server sql_使用SQL Server中的“Pivot”将行转换为列
  9. android viewpager监听,viewPager的OnPageChangeListener监听器的方法调用顺序
  10. 计算机显示无法格式化,计算机格式化SD卡提示Windows无法完成格式化解决方法
  11. PHPEclipse安装与使用
  12. win10和win11系统,手机或者其他设备连接不上电脑热点,一直在转圈圈的解决方法
  13. 【正点原子FPGA连载】第十二章呼吸灯实验 -摘自【正点原子】新起点之FPGA开发指南_V2.1
  14. 手机投屏不是全屏怎么办_手机投屏win10怎么退出全屏详细图文教程
  15. 相关矩阵 Correlation matrix
  16. Matlab基础——变量和语句
  17. 为什么你学不会递归?告别递归,谈谈我的经验
  18. 在html中如何做个人微博,学习记录:爬取个人微博
  19. 电商API_拼多多商品详细
  20. 榜样访谈——董宇航:在俱乐部中收获爱情

热门文章

  1. android黑窗口获取md5_Android获取文件的MD5
  2. python解码base64_在python中解码Base64 Gzip
  3. java int数组写入文件中_Java程序将int数组写入文件
  4. mysql sort aborted_mysql排序中断(Sort aborted)-mysql临时文件无法写入
  5. 【若依(ruoyi)】jQuery.validator
  6. date样式找不到_涡轮+国VI排放,顶配售价不到12万,家用轿车看它准没错
  7. crashdumpandroid_Android 中Crash时如何获取异常信息详解及实例
  8. 简单实现x的n次方pta_学会这四招,原来平均值计算也可以这么简单
  9. 使用ajax实现无刷新邮箱验证码,AJAX和WebService实现邮箱验证(无刷新)
  10. java创建一个人函数类_Java对象和类–学习如何创建和实现