程序类型
1、语言:C#
2、框架:.NET Framework
3、类型:Window窗体应用
程序说明
1、主窗口设置或者读取子窗口数据时,通过调用子窗口实例的方法实现的。
2、子窗口设置或者读取主窗口数据时,通过将主窗口的方法传递给子窗口实例的委托实现的。
程序代码
MainForm代码
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;
using System.Media;
namespace WindowsMessageExchange
{
public partial class MainForm : Form
{
private ChildrenForm myChildrenForm = null;
public MainForm()
{
InitializeComponent();
}
public void SetTextValue(string setStr)
{
this.label_ChildrenFormSetValue.Text = setStr;
}
public string ReadTextValue( )
{
return(this.richTextBox_MainFormValue.Text);
}
public void EnableOpenChildmrenFormt()
{
button_OpenChildrenForm.Enabled=true;
}
private void button_OpenChildrenForm_Click(object sender, EventArgs e)
{
myChildrenForm = ChildrenForm.CreateInstance();//实例化子窗口
myChildrenForm.ReadMainFormTextDelegate = this.ReadTextValue;//设置子窗口委托,用来读取主窗口值
myChildrenForm.SetMainFormTextDelegate = this.SetTextValue;//设置子窗口委托,用来设置主窗口值
myChildrenForm.EnableOpenChildmrenFormtDelegate = this.EnableOpenChildmrenFormt;
//myChildrenForm.StartPosition = FormStartPosition.CenterParent;
//myChildrenForm.StartPosition = FormStartPosition.CenterScreen;
myChildrenForm.StartPosition = FormStartPosition.Manual;
int xWidth = SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度
int yHeight = SystemInformation.PrimaryMonitorSize.Height;//获取显示器屏幕高度
int formWidth = myChildrenForm.Size.Width;//获取窗体宽度
int formHeight = myChildrenForm.Size.Height;//获取窗体高度
myChildrenForm.Location = new Point((xWidth-formWidth) / 2, (yHeight - formHeight) / 2);//居中显示
//myChildrenForm.ShowDialog();//模态窗口显示
myChildrenForm.Show();//非模态窗口显示
SystemSounds.Asterisk.Play();//使用系统提示音
myChildrenForm.Focus();//聚焦子窗口
this.button_OpenChildrenForm.Enabled = false;
}
private void button_setChildrenFormText_Click(object sender, EventArgs e)
{
myChildrenForm.SetTextValue(this.richTextBox_setTextValue.Text);
}
private void button_ReadChilerenText_Click(object sender, EventArgs e)
{
this.richTextBox_readValue.Text= myChildrenForm.ReadTextValue();
}
}
}
ChildrenForm代码
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 WindowsMessageExchange
{
public partial class ChildrenForm : Form
{
public delegate string ReadMainFormText();
public ReadMainFormText ReadMainFormTextDelegate = null;
public delegate void SetMainFormText(string setStr);
public SetMainFormText SetMainFormTextDelegate = null;
public delegate void EnableOpenChildmrenForm();
public EnableOpenChildmrenForm EnableOpenChildmrenFormtDelegate = null;
private static ChildrenForm _instance = null;
/// <summary>
/// 静态方法,创建单实例
/// </summary>
/// <returns></returns>
public static ChildrenForm CreateInstance()
{
if (_instance == null)
{
_instance = new ChildrenForm();
}
return _instance;
}
private ChildrenForm()
{
InitializeComponent();
}
public void SetTextValue(string setStr)
{
this.label_MainFormSetValue.Text = setStr;
}
public string ReadTextValue()
{
return( this.richTextBox_ChildrenFormValue.Text);
}
private void button_setMainFormText_Click(object sender, EventArgs e)
{
if (SetMainFormTextDelegate != null)
{
SetMainFormTextDelegate(richTextBox_setTextValue.Text);
}
}
private void button_ReadMainFormText_Click(object sender, EventArgs e)
{
if (ReadMainFormTextDelegate != null)
{
richTextBox_readValue.Text = ReadMainFormTextDelegate();
}
}
private void ChildrenForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (EnableOpenChildmrenFormtDelegate != null)
{
EnableOpenChildmrenFormtDelegate();
}
}
/// <summary>
/// 关闭窗口前,清空实例,让下次可以成功实例化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChildrenForm_FormClosing(object sender, FormClosingEventArgs e)
{
_instance = null;
}
}
}
主窗口与子窗口数据对应关系
程序代码下载链接
源程序下载地址: https://download.csdn.net/download/lyrain2009/12034049.