zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    C#中using的使用-以FileStream写入文件为例
    C#中FileStream的对比以及使用方法
    DevExpress的TextEdit、RadioGroup、ColorPickEdit设置默认值
    DevExpress的TextEdit限制输入内容的格式,比如只能输入数字
    CS中委托与事件的使用-以Winform中跨窗体传值为例
    Winforn中怎样在窗体中打开另一个窗体
    Winforn中实现ZedGraph自定义添加右键菜单项(附源码下载)
    Winform中实现ZedGraph的多条Y轴(附源码下载)
    《深入理解Java虚拟机》内存分配策略
    三十而立
  • 原文地址:https://www.cnblogs.com/xe2011/p/3440280.html
Copyright © 2011-2022 走看看