zoukankan      html  css  js  c++  java
  • C# 委托 父窗口与子窗口间传值

    1)目标

    父窗口与子窗口都有1个Button和1个Label。

    目标1:单击父窗口的Button,子窗口的Label将显示父窗口传来的值。

    目标2:单击子窗口的Button,父窗口的Label将显示子窗口传来的值。

    2)父窗口代码

    using System;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp5
    {
        public delegate void ShowMessageService(string msg);
        public partial class FormMain : Form
        {
            private FormChild childForm = new FormChild();
            public FormMain()
            {
                InitializeComponent();
            }
    
            private void FormMain_Load(object sender, EventArgs e)
            {
                childForm.showMessageChild = new ShowMessageService(UpdateLabel);
                childForm.Show();
            }
    
            public async void UpdateLabel(string msg)
            {
                await Task.Delay(2000);
                this.label1.Text = msg;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                ShowMessageService sms = new ShowMessageService(childForm.UpdateLabel);
                this.BeginInvoke(sms, "Message from FormMain");
            }
    
        }
    }
    

    3)子窗口代码

    using System;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp5
    {
        public partial class FormChild : Form
        {
            public ShowMessageService showMessageChild;
            public FormChild()
            {
                InitializeComponent();
            }
    
            public async void UpdateLabel(string msg)
            {
                await Task.Delay(2000);
                this.label1.Text = msg;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.BeginInvoke(showMessageChild, "Message from FormChild");
            }
        }
    }
    

     4)解释

    4.1)定义委托

    public delegate void ShowMessageService(string msg);

    4.2)父窗口向子窗口传递消息

    ①由于父窗口拥有子窗口对象,所以直接利用子窗口的方法来创建委托对象,然后用this.BeginInvoke进行异步调用。

            private void button1_Click(object sender, EventArgs e)
            {
                ShowMessageService sms = new ShowMessageService(childForm.UpdateLabel);//定义委托对象,指向子窗口的UpdateLabel方法。
                this.BeginInvoke(sms, "Message from FormMain"); //执行的是子窗口的UpdateLabel方法
            }

    4.3)子窗口向父窗口传递消息

    ①子窗口声明委托对象

        public partial class FormChild : Form
        {

           public ShowMessageService showMessageChild;//声明

        }

    ②在主窗口给委托对象赋值。

        public partial class FormMain : Form
        {
            private FormChild childForm = new FormChild();
            private void FormMain_Load(object sender, EventArgs e)
            {
                childForm.showMessageChild = new ShowMessageService(UpdateLabel); //子窗口的委托对象指向父窗口的UpdateLabel方法。
                childForm.Show();
            }

        }

    ③子窗口用this.BeginInvoke进行异步调用。

             private void button1_Click(object sender, EventArgs e)
            {
                this.BeginInvoke(showMessageChild, "Message from FormChild");
            }

  • 相关阅读:
    JAVAWEB使用保存cookie、删除cookie、获取cookie工具类
    JAVA比较指定的两个日期
    编写一个C程序运行时输出 Hello World!
    正确理解以下名词及其含义:1源程序,目标程序,可执行程序2程序编辑,程序编译,程序连接3程序,程序模块,程序文件4函数,主函数,被调用函数,库函数5程序调试,程序测试
    为什么需要计算机语言?高级语言有哪些特点?
    什么是程序?什么是程序设计?
    题解 卡农
    题解 GT考试
    题解 SP6779 【GSS7
    题解 Crash 的文明世界
  • 原文地址:https://www.cnblogs.com/wyvern0618/p/9442381.html
Copyright © 2011-2022 走看看