zoukankan      html  css  js  c++  java
  • C# 委托例子

    两个子窗口向一个主窗口发送信息

    主窗口:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Form2 fm2 = new Form2();
                fm2.msgSender = this.Receiver; //[3]将委托对象和方法关联
                fm2.Show();
    
                Form3 fm3 = new Form3();
                fm3.msgSender = this.Receiver; //[3]将委托对象和方法关联
                fm3.Show();
            }
    
            //[2]根据委托定义方法完成数据传递
            public void Receiver(string info)
            {
                this.label2.Text = info;
            }
       
        }
            //[1]声明委托,一般定义在外面
            public delegate void ShowInfo(string param);
    }

    子窗口1:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
            //创建委托对象
            public ShowInfo msgSender;
    
    
            //通过委托传递数据
            private void button1_Click(object sender, EventArgs e)
            {
                msgSender(this.textBox1.Text);
            }
     
        }
    }

    子窗口2:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            //创建委托对象[从到主]
            public ShowInfo msgSender;
    
            private void button1_Click(object sender, EventArgs e)
            {
                msgSender(this.textBox1.Text);
            }
             
        }
    }

    主窗口向两个子窗口发送信息

    上代码~~~~~~~~~~

    主窗口:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
    
            public Action<string> SendMsg { get; set; }
    
            public Form1()
            {
                InitializeComponent();
                Form2 fm2 = new Form2();
                SendMsg += fm2.Recevier;
                fm2.Show();
    
                Form3 fm3 = new Form3();
                SendMsg += fm3.Receiver; //[3]将委托对象和方法关联
                fm3.Show();
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                if (SendMsg != null)
                {
                    SendMsg(this.textBox1.Text);//执行所有注册的委托
                }
            }
    
    
        }
    
    }

    子窗口1:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            public void Recevier(string info)
            {
                this.label1.Text = info;
            }
        }
         
    }

    子窗口2:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            public void Receiver(string info)
            {
                this.label1.Text = info;
            }
    
        }
     
    }
  • 相关阅读:
    单片机模块化程序: 来看看加入环形队列的串口发送数据
    单片机模块化程序: 丢给你个环形队列玩玩
    单片机模块化程序: 你是否还是个小学生时代的串口发送数据
    单片机模块化程序: 关于串口接收处理数据
    单片机模块化程序: 看看是不是你想要的按键处理
    单片机模块化程序: 来看下我的程序架子吧
    ESA2GJK1DH1K升级篇: 升级STM32 预热: 单片机每隔一定时间 使用 http 获取天气
    ESA2GJK1DH1K升级篇: 升级STM32 预热: 单片机定时 使用 http 获取云端文本文件里面的内容,然后显示在液晶屏
    ESA2GJK1DH1K升级篇: 远程升级准备工作: 使用TCP客户端连接Web服务器实现http下载数据
    ESA2GJK1DH1K升级篇: 远程升级准备工作: 安装Web服务器
  • 原文地址:https://www.cnblogs.com/youmingkuang/p/7452223.html
Copyright © 2011-2022 走看看