winform程序三个窗体间同步数据(六):利用多播委托同步数据

一 显示效果

二  代码

1 入口程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
       
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
           ParentFrm parentFrm = new ParentFrm();
           Application.Run(parentFrm);//启动父窗体
        }
    }
}

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;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class ParentFrm : Form, InterfaceObserver
    {
        public ParentFrm()
        {
            InitializeComponent();
        }
        public TextBox getTextBox()
        {
            return this.TbParent;//TbParent 是私有的对象 ,所以要有公有的方法获取TbParent对象
        }
        private void button1_Click(object sender, EventArgs e)//点击事件
        {
            ChildFrm childFrm = new ChildFrm();
            ChildForm1 childForm1 = new ChildForm1();
            childFrm.Show();//显示子窗体
            childForm1.Show();//显示子窗体1
            childFrm.del += childForm1.getTextBox;将childForm1对象中的方法注册到childFrm类的委托对象中
            childFrm.del += this.getTextBox;//将ParentFrm类中的方法注册到childFrm类的委托对象中
        
        }
    }
}

3 子窗体

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 delegate TextBox MyDelegate();
    public partial class ChildFrm : Form
    {
        public MyDelegate del;
        public List<InterfaceObserver> ListObserver { get; set; }
        public ChildFrm()
        {
            InitializeComponent();
            this.ListObserver = new List<InterfaceObserver>();
        }

        private void TbChild_TextChanged(object sender, EventArgs e)
        {          
           // del().Text = this.TbChild.Text;  不能这样用,这样调用只能同步到添加的最后一个窗口。应该用下面的方法
            Delegate[] list = del.GetInvocationList();//委托对象数组
            foreach (MyDelegate MyDel in list)
            {
                MyDel().Text = this.TbChild.Text;//MyDel()调用委托并获得返回对象TEXTBOX,并个其赋值
            }
        }
    }
}

4 兄弟窗体

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 ChildForm1 : Form, InterfaceObserver
    {
        
        public TextBox getTextBox()
        {
            return this.textBox1;
        }
        public ChildForm1()
        {
            InitializeComponent();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值