在设计窗体程序时往往需要相互调用的窗体间传递复杂的数据,有时候甚至需要子窗体修改父窗体的内容.前一阵在博客园中看到很多人讨论这个问题,在海天一鸥《窗体间传值和窗体间互操作》的评论中,我看到有这么几种做法:1)公开一个静态变量;2)在子窗体中创建一个公有字段;3)在父窗体中使用委托与事件;4)将子窗体作为父窗体成员.
这些办法我感觉都不是特别好,会导致父窗体与子窗体耦合过于紧密,对任何一个窗体的修改需要重新编译另外一个窗体.根据"依赖倒置”的原则,通过引入一个结果对象,就可以避免这种紧耦合,同时也可以传递任意复杂的数据.如果需要在子窗体中改变父窗体状态,也可以在这个结果对象中定义委托与事件来达到目的.我在这里给出我的解决方案.
首先定义一个结果对象,用来存放子窗体返回的结果.同时定义一些事件,可以让子窗体修改父窗体的状态.代码如下:
using System;
namespace WinParam
{
public delegate void TextChangedHandler(string s);
public class cResult
{
public string Result1 = "";
public string Result2 = "";
public event TextChangedHandler TextChanged;
public void ChangeText(string s)
{
if(TextChanged != null)
TextChanged(s);
}
}
}
添加一子窗体构造函数,允许接收一结果对象:
private cResult r;
public frmChild(cResult r):this()
{
this.r = r;
}
在父窗体中创建子窗体,并订阅cResult事件:
private void btnCallChild_Click(object sender, System.EventArgs e)
{
cResult r = new cResult();
r.TextChanged += new TextChangedHandler(this.EventResultChanged);
frmChild fc = new frmChild(r);
fc.ShowDialog();
txtCallResult.Text = "The Result is: " + r.Result1 + " " + r.Result2;
}
private void EventResultChanged(string s)
{
txtEventResult.Text = s;
}
这样确保父窗体知道子窗体,而子窗体不知道父窗体.父窗体改变后不需要重新编译子窗体.同时两个窗体都依赖于结果对象,结果对象的稳定性也决定了父窗体与子窗体关系的稳定性.下面是程序运行结果:
注:提供的代码仅仅是功能演示,如果实际使用需要添加一些额外辅助代码(对象释放、取消事件订阅等).
所有源代码一起转贴如下:
Main.cs
using System;
using System.Windows.Forms;
namespace WinParam
{
public class AppMain
{
///
/// 应用程序的主入口点.
///
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
}
}
MainForm.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WinParam
{
///
/// Form1 的摘要说明.
///
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtCallResult;
private System.Windows.Forms.Button btnCallChild;
private System.Windows.Forms.TextBox txtEventResult;
///
/// 必需的设计器变量.
///
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void InitializeComponent()
{
this.txtCallResult = new System.Windows.Forms.TextBox();
this.btnCallChild = new System.Windows.Forms.Button();
this.txtEventResult = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// txtCallResult
//
this.txtCallResult.Location = new System.Drawing.Point(8, 48);
this.txtCallResult.Multiline = true;
this.txtCallResult.Name = "txtCallResult";
this.txtCallResult.Size = new System.Drawing.Size(368, 176);
this.txtCallResult.TabIndex = 5;
this.txtCallResult.Text = "";
//
// btnCallChild
//
this.btnCallChild.Location = new System.Drawing.Point(264, 8);
this.btnCallChild.Name = "btnCallChild";
this.btnCallChild.Size = new System.Drawing.Size(112, 23);
this.btnCallChild.TabIndex = 4;
this.btnCallChild.Text = "Call ChildForm";
this.btnCallChild.Click += new System.EventHandler(this.btnCallChild_Click);
//
// txtEventResult
//
this.txtEventResult.Location = new System.Drawing.Point(8, 8);
this.txtEventResult.Name = "txtEventResult";
this.txtEventResult.Size = new System.Drawing.Size(240, 21);
this.txtEventResult.TabIndex = 3;
this.txtEventResult.Text = "";
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 237);
this.Controls.Add(this.txtCallResult);
this.Controls.Add(this.btnCallChild);
this.Controls.Add(this.txtEventResult);
this.Name = "frmMain";
this.Text = "主窗体";
this.ResumeLayout(false);
}
#endregion
private void btnCallChild_Click(object sender, System.EventArgs e)
{
cResult r = new cResult();
r.TextChanged += new TextChangedHandler(this.EventResultChanged);
frmChild fc = new frmChild(r);
fc.ShowDialog();
txtCallResult.Text = "The Result is:rn" + r.Result1 + "rn" + r.Result2;
}
private void EventResultChanged(string s)
{
txtEventResult.Text = s;
}
}
}
ChildForm.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WinParam
{
///
/// ChildForm 的摘要说明.
///
public class frmChild : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.RadioButton optThree;
private System.Windows.Forms.RadioButton optOne;
private System.Windows.Forms.RadioButton optTwo;
private System.Windows.Forms.TextBox txtResult2;
private System.Windows.Forms.TextBox txtResult1;
private System.Windows.Forms.Button cmdOk;
///
/// 必需的设计器变量.
///
private System.ComponentModel.Container components = null;
public frmChild()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
private cResult r;
public frmChild(cResult r):this()
{
this.r = r;
}
///
/// 清理所有正在使用的资源.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.optThree = new System.Windows.Forms.RadioButton();
this.optOne = new System.Windows.Forms.RadioButton();
this.optTwo = new System.Windows.Forms.RadioButton();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.txtResult2 = new System.Windows.Forms.TextBox();
this.txtResult1 = new System.Windows.Forms.TextBox();
this.cmdOk = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.optThree);
this.groupBox1.Controls.Add(this.optOne);
this.groupBox1.Controls.Add(this.optTwo);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(144, 104);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "修改父窗体值";
//
// optThree
//
this.optThree.Location = new System.Drawing.Point(16, 72);
this.optThree.Name = "optThree";
this.optThree.Size = new System.Drawing.Size(104, 16);
this.optThree.TabIndex = 7;
this.optThree.Text = "Show "Three"";
this.optThree.CheckedChanged += new System.EventHandler(this.optThree_CheckedChanged);
//
// optOne
//
this.optOne.Location = new System.Drawing.Point(16, 24);
this.optOne.Name = "optOne";
this.optOne.Size = new System.Drawing.Size(104, 16);
this.optOne.TabIndex = 6;
this.optOne.Text = "Show "One"";
this.optOne.CheckedChanged += new System.EventHandler(this.optOne_CheckedChanged);
//
// optTwo
//
this.optTwo.Location = new System.Drawing.Point(16, 48);
this.optTwo.Name = "optTwo";
this.optTwo.Size = new System.Drawing.Size(104, 16);
this.optTwo.TabIndex = 5;
this.optTwo.Text = "Show "Two"";
this.optTwo.CheckedChanged += new System.EventHandler(this.optTwo_CheckedChanged);
//
// groupBox2
//
this.groupBox2.Controls.Add(this.txtResult2);
this.groupBox2.Controls.Add(this.txtResult1);
this.groupBox2.Location = new System.Drawing.Point(160, 8);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(136, 104);
this.groupBox2.TabIndex = 6;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "回传的结果";
//
// txtResult2
//
this.txtResult2.Location = new System.Drawing.Point(16, 64);
this.txtResult2.Name = "txtResult2";
this.txtResult2.TabIndex = 3;
this.txtResult2.Text = "Result(2)";
//
// txtResult1
//
this.txtResult1.Location = new System.Drawing.Point(16, 32);
this.txtResult1.Name = "txtResult1";
this.txtResult1.TabIndex = 2;
this.txtResult1.Text = "Result(1)";
//
// cmdOk
//
this.cmdOk.DialogResult = System.Windows.Forms.DialogResult.OK;
this.cmdOk.Location = new System.Drawing.Point(224, 128);
this.cmdOk.Name = "cmdOk";
this.cmdOk.TabIndex = 7;
this.cmdOk.Text = "确定(&O)";
this.cmdOk.Click += new System.EventHandler(this.cmdOk_Click);
//
// frmChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(304, 157);
this.Controls.Add(this.cmdOk);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "frmChild";
this.Text = "子窗体";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void optOne_CheckedChanged(object sender, System.EventArgs e)
{
r.ChangeText("One");
}
private void optTwo_CheckedChanged(object sender, System.EventArgs e)
{
r.ChangeText("Two");
}
private void optThree_CheckedChanged(object sender, System.EventArgs e)
{
r.ChangeText("Three");
}
private void cmdOk_Click(object sender, System.EventArgs e)
{
r.Result1 = txtResult1.Text;
r.Result2 = txtResult2.Text;
}
}
}
cResult.cs
using System;
namespace WinParam
{
public delegate void TextChangedHandler(string s);
public class cResult
{
public string Result1 = "";
public string Result2 = "";
public event TextChangedHandler TextChanged;
public void ChangeText(string s)
{
if(TextChanged != null)
TextChanged(s);
}
}
}
本文作者:http://yinzq.itpub.net/ 来源:西桥人家
CIO之家 www.ciozj.com 微信公众号:imciow