父窗体FormBusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Reflection;namespace WindowsFormsApplication1 {
public partial class FormB : Form { public FormB() { InitializeComponent(); } private void FormB_Load(object sender, EventArgs e) { textBox1.TextChanged += new EventHandler(textBox1_TextChanged); } void textBox1_TextChanged(object sender, object msg) { MiddleModule.SendMessage(this, this.textBox1.Text); } private void button1_Click(object sender, EventArgs e) { FormC fc = new FormC(); fc.Show(); } } /// <summary> /// 定义发布消息的委托 /// </summary> /// <param name="sender">发布者</param> /// <param name="msg">消息</param> public delegate void Send(object sender, object msg); /// <summary> /// 观察者的中间模块组建 /// </summary> public class MiddleModule { /// <summary> ///消息发布的事件 /// </summary> public static event Send eventSend; public static void SendMessage(object sender, object msg) { if (eventSend != null) { eventSend(sender, msg); } } } } 子窗体FormCusing 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 FormC : Form { public FormC() { InitializeComponent(); MiddleModule.eventSend += new Send(MiddleModule_eventSend); }
void MiddleModule_eventSend(object sender, object msg) { FormB f = sender as FormB; if (null != f) { this.textBox1.Text = msg.ToString(); } } } }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;namespace WindowsFormsApplication1
{
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
} private void FormB_Load(object sender, EventArgs e)
{
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
} void textBox1_TextChanged(object sender, object msg)
{
MiddleModule.SendMessage(this, this.textBox1.Text);
} private void button1_Click(object sender, EventArgs e)
{
FormC fc = new FormC();
fc.Show();
}
} /// <summary>
/// 定义发布消息的委托
/// </summary>
/// <param name="sender">发布者</param>
/// <param name="msg">消息</param>
public delegate void Send(object sender, object msg);
/// <summary>
/// 观察者的中间模块组建
/// </summary>
public class MiddleModule
{
/// <summary>
///消息发布的事件
/// </summary>
public static event Send eventSend; public static void SendMessage(object sender, object msg)
{
if (eventSend != null)
{
eventSend(sender, msg);
}
}
}
}
子窗体FormCusing 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 FormC : Form
{
public FormC()
{
InitializeComponent();
MiddleModule.eventSend += new Send(MiddleModule_eventSend);
}
void MiddleModule_eventSend(object sender, object msg)
{
FormB f = sender as FormB;
if (null != f)
{
this.textBox1.Text = msg.ToString();
}
}
}
}