ListData1;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
}
有人会问,为什么不把上面的代码放到构造函数里面去呢?如下不是更好,
public Form2()
{
InitializeComponent();
Form1 pareForm = (Form1)this.Owner;
this.listData2 = pareForm.ListData1;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
}
那我会对你说错了,因为在主窗体修改按钮被点击后,开始执行
Form2 formChild = new Form2();
而在Form2的实例化过程中会在构造函数中执行
Form1 pareForm = (Form1)this.Owner;
而这时的this.Owner是没有值的,为空引用,那么下面的代码肯定也出问题,
this.listData2 = pareForm.ListData1;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
当整个Form2实例化完成后,才会执行
formChild.Owner = this;
这条代码,所以使用了Form2_Load事件。
那怎样可以不使用Form2_Load事件呢?等下面我们来修改代码实现它。
下面的子窗体代码没有变化,
private void buttonAdd_Click(object sender, System.EventArgs e)
{
if(this.textBoxAdd.Text.Trim().Length>0)
{
this.listData2.Add(this.textBoxAdd.Text.Trim());
this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());
}
else
MessageBox.Show("请输入添加的内容!");
}
private void buttonDel_Click(object sender, System.EventArgs e)
{
int index = this.listBoxFrm2.SelectedIndex;
if(index!=-1)
{
this.listData2.RemoveAt(index);
this.listBoxFrm2.Items.RemoveAt(index);
}
else
MessageBox.Show("请选择删除项!");
}
private void buttonOK_Click(object sender, System.EventArgs e)
{
this.Close();
}
好了,结果同第一篇中的一样,子窗体能修改主窗体的值。
2.使用自定义属性或方法
下面我们来讲讲怎样使用自定义属性或方法来完成数据修改功能而不使用Form2_Load事件。
主窗体的修改按钮点击处理函数如下:
private void buttonEdit_Click(object sender, System.EventArgs e)
private void buttonEdit_Click(object sender, System.EventArgs e)
{
Form2 formChild = new Form2();
formChild.ListData2 = this.listData1;
formChild.ShowDialog();
this.listBoxFrm1.DataSource = null;
this.listBoxFrm1.DataSource = this.listData1;
}
并且我们去掉了主窗体的ListData1属性,
//public ArrayList ListData1
//{
// get{return this.listData1;}
//}
而在子窗体中加上ListData2属性,
public ArrayList ListData2
{
set
{
this.listData2 = value;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
}
}
也可以把属性改成方法,
public void SetListData(ArrayList listData)
{
this.listData2 = listData;
foreach(object o in this.listData2)
this.listBoxFrm2.Items.Add(o);
}
而在主窗体的修改按钮处理函数中也要相应改动:
formChild.ListData2 = this.listData1;
改为
formChild.SetListData(this.listData1);
总结,我们通过Form类的Owner属性来建立主从窗体间的桥梁,这个是不是类似于把主窗体作为子窗体的构造函数参数传入实现的功能差不多;另外又采用了属性和方法来完成数据的交互,我觉得这种实现方法很实用,特别是用在不需要实例化类或着已经有了实例的情况下传递数据。下一篇文章我们来讲如何使用静态类来完成数据的交互。
三.使用静态类
下面是定义的一个类:
using System;
using System.Collections;
namespace ZZ
{
public class AppDatas
{
private static ArrayList listData;
static AppDatas()
{
listData = new ArrayList();
listData.Add("DotNet");
listData.Add("C#");
listData.Add("Asp.net");
listData.Add("WebService");
listData.Add("XML");
}
public static ArrayList ListData
{
get{return listData;}
}
public static ArrayList GetListData()
{
return listData;
}
}
}
上面包含了一个静态类成员,listData,一个静态构造函数static AppDatas(),用来初始化listData的数据。还有一个静态属性ListData和一个静态GetListData()方法,他们实现了同样的功能就是返回listData。
由于前面两篇文章已经讲了很多,这里不细说了,下面是完整的代码:
Form1.cs文件
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace ZZ
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonEdit;
private System.Windows.Forms.ListBox listBoxFrm1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
this.listBoxFrm1.DataSource = AppDatas.ListData;
}
protected override void Dispose( bool disposing )
{
if( disposing )
if(components != null)
components.Dispose();
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void InitializeComponent()
{
this.buttonEdit = new System.Windows.Forms.Button();
this.listBoxFrm1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
this.buttonEdit.Location = new System.Drawing.Point(128, 108);
this.buttonEdit.Name = "buttonEdit";
this.buttonEdit.TabIndex = 1;
this.buttonEdit.Text = "修改";
this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);
this.listBoxFrm1.ItemHeight = 12;
this.listBoxFrm1.Location = new System.Drawing.Point(12, 8);
this.listBoxFrm1.Name = "listBoxFrm1";
this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);
this.listBoxFrm1.TabIndex = 2;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(208, 141);
this.Controls.Add(this.listBoxFrm1);
this.Controls.Add(this.buttonEdit);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
private void buttonEdit_Click(object sender, System.EventArgs e)
{
Form2 formChild = new Form2();
formChild.ShowDialog();
this.listBoxFrm1.DataSource = null;
this.listBoxFrm1.DataSource = AppDatas.ListData;
}
}
}
Form2.cs文件
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace ZZ
{
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button buttonOK;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ListBox listBoxFrm2;
private System.Windows.Forms.Button buttonAdd;
private System.Windows.Forms.Button buttonDel;
private System.Windows.Forms.TextBox textBoxAdd;
public Form2()
{
InitializeComponent();
foreach(object o in AppDatas.ListData)
this.listBoxFrm2.Items.Add(o);
}
protected override void Dispose( bool disposing )
{
if( disposing )
if(components != null)
components.Dispose();
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.buttonOK = new System.Windows.Forms.Button();
this.listBoxFrm2 = new System.Windows.Forms.ListBox();
this.buttonAdd = new System.Windows.Forms.Button();
this.buttonDel = new System.Windows.Forms.Button();
this.textBoxAdd = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.buttonOK.Location = new System.Drawing.Point(188, 108);
this.buttonOK.Name = "buttonOK";
this.buttonOK.TabIndex = 0;
this.buttonOK.Text = "确定";
this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
this.listBoxFrm2.ItemHeight = 12;
this.listBoxFrm2.Location = new System.Drawing.Point(8, 8);
this.listBoxFrm2.Name = "listBoxFrm2";
this.listBoxFrm2.Size = new System.Drawing.Size(168, 124);
this.listBoxFrm2.TabIndex = 2;
this.buttonAdd.Location = new System.Drawing.Point(188, 44);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.TabIndex = 3;
this.buttonAdd.Text = "增加";
this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
this.buttonDel.Location = new System.Drawing.Point(188, 76);
this.buttonDel.Name = "buttonDel";
this.buttonDel.TabIndex = 4;
this.buttonDel.Text = "删除";
this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click);
this.textBoxAdd.Location = new System.Drawing.Point(188, 12);
this.textBoxAdd.Name = "textBoxAdd";
this.textBoxAdd.Size = new System.Drawing.Size(76, 21);
this.textBoxAdd.TabIndex = 5;
this.textBoxAdd.Text = "";
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 141);
this.Controls.Add(this.textBoxAdd);
this.Controls.Add(this.buttonDel);
this.Controls.Add(this.buttonAdd);
this.Controls.Add(this.listBoxFrm2);
this.Controls.Add(this.buttonOK);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
private void buttonOK_Click(object sender, System.EventArgs e)
{
this.Close();
}
{
this.Close();
}
private void buttonAdd_Click(object sender, System.EventArgs e)
{
if(this.textBoxAdd.Text.Trim().Length>0)
{
AppDatas.ListData.Add(this.textBoxAdd.Text.Trim());
this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());
}
else
MessageBox.Show("请输入添加的内容!");
}
private void buttonDel_Click(object sender, System.EventArgs e)
{
int index = this.listBoxFrm2.SelectedIndex;
if(index!=-1)
{
AppDatas.ListData.RemoveAt(index);
this.listBoxFrm2.Items.RemoveAt(index);
}
else
MessageBox.Show("请选择删除项!");
}
}
}
总结,我认为使用静态类比较多的地方就是把应用程序的配置文件装载到一个静态类里面,让所有的窗体和其他实例都可以通过静态属性以及静态方法使用这些数据,比如三层结构或多层结构都可以访问它,而不是在多个实例间传来传去。在这里我们讨论的是Windows窗体,其实在两个不同的实例间交互数据,都可以采用三篇文章中的方案实现,除非是这个类特有的属性或着方法。现在都讲完了,虽然不是什么高深的东西,但是希望能对一些初学者有所帮助,要是能真正的能解决一些朋友的实际问题,也算是我没有浪费时间来写文章,同时也欢迎各位朋友进行技术交流,共同提高。