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


    附件:http://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
  • 相关阅读:
    关于GitHub推送时发生Permission denied (publickey)的问题
    线性模型——机器学习(西瓜书)读书笔记
    梯度下降算法的简单理解
    PRML学习笔记第一章
    python函数学习之装饰器
    机器学习 概论
    Mybatis
    Nginx 常用配置清单
    接口,抽象类
    IntelliJ IDEA打war包
  • 原文地址:https://www.cnblogs.com/xe2011/p/3440280.html
Copyright © 2020-2023  润新知