架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 17469|回复: 1

[WinForm] 在Winform 窗体之间 委托传值

[复制链接]
发表于 2015-12-28 12:27:20 | 显示全部楼层 |阅读模式
第一个窗体(也就是主窗体):

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace winformDelegate
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }

  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.            Form2 frm=new Form2();
  20.            frm.Myevent += GetValue;
  21.             frm.Show();
  22.         }

  23.         public void GetValue(string text)
  24.         {
  25.             textBox1.Text = text;
  26.         }
  27.     }
  28. }
复制代码


第二个窗体:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;

  9. namespace winformDelegate
  10. {
  11.     public partial class Form2 : Form
  12.     {
  13.         public delegate void DelegaeValue
  14.             (string text);

  15.         public event DelegaeValue Myevent;

  16.         protected virtual void ValueInformation(string text)
  17.         {
  18.             DelegaeValue handler = Myevent;
  19.             if (handler != null)
  20.                 handler(text);
  21.         }

  22.         public Form2()
  23.         {
  24.             InitializeComponent();
  25.         }

  26.         private void button1_Click(object sender, EventArgs e)
  27.         {
  28.             if (Myevent != null)
  29.             {
  30.             }
  31.             ValueInformation(textBox1.Text);
  32.         }


  33.     }
  34. }
复制代码






上一篇:form的onsubmit事件--表单提交前的验证最佳实现方式
下一篇:浅析JSONP-解决Ajax跨域访问问题
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
 楼主| 发表于 2017-8-17 11:28:12 | 显示全部楼层
frm.gif

不用委托也能传值

窗体一代码:



窗体二代码:



码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2026-1-3 01:34

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表