C# 动态创建出来的窗体间的通讯 delegate2

附件:https://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate02.rar

 

窗体2 和窗体3 都是动态创建出来的

现在 FORM3.TEXT要即时 = FORM2.TEXT

 

FORM1窗体代码

 

        Form2 f2 = new Form2();
        Form3 f3 = new Form3();
        private void Form1_Load(object sender, EventArgs e)
        {
            f2.XYZ += new Form2.CallBack(GetForm2TextBox1Text);

            f2.Dock = DockStyle.Fill;
            f2.TopLevel = false;
            f2.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            f2.Parent = panel1;
            f2.Show();

            f3.Dock = DockStyle.Fill;
            f3.TopLevel = false;
            f3.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            panel2.Controls.Add(f3);
            f3.Show();         
        }

        private void GetForm2TextBox1Text(string s)
        {
            f3.textBox1.Text = s;
        }

 

FORM2窗体代码

 

        public delegate void CallBack(string s);
        public event CallBack XYZ;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            XYZ(textBox1.Text);
        }

 

FORM3窗体代码

 无

 

FORM1窗体代码

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Form2 f2 = new Form2();
        Form3 f3 = new Form3();
        private void Form1_Load(object sender, EventArgs e)
        {
            f2.XYZ += new Form2.CallBack(GetForm2TextBox1Text);

            f2.Dock = DockStyle.Fill;
            f2.TopLevel = false;
            f2.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            f2.Parent = panel1;
            f2.Show();

            f3.Dock = DockStyle.Fill;
            f3.TopLevel = false;
            f3.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            panel2.Controls.Add(f3);
            f3.Show();         
        }

        private void GetForm2TextBox1Text(string s)
        {
            f3.textBox1.Text = s;
        }
    }
}
View Code

 

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;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        
        public delegate void CallBack(string s);
        public event CallBack XYZ;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            XYZ(textBox1.Text);
        }
    }
}
View Code

 

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 WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
    }
}
View Code

 

 

 

 

posted @ 2013-11-24 17:51  XE2011  阅读(307)  评论(0编辑  收藏  举报