在.net中轻松掌握Windows窗体间的数据交互(二)

来源:互联网 发布:纳粹飞碟 知乎 编辑:程序博客网 时间:2024/04/30 11:54
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>


在.net中轻松掌握Windows窗体间数据交互(二)

zhzuo(秋枫)

《在.net中轻松掌握Windows窗体间数据交互(一)》一文中我们讲了使用带参数的构造函数来实现窗体间的数据传递,我认为是用的比较多的一种,接下来让我们看看另外两种实现方法。

二.给窗体添加属性或方法

1.使用Form类的Owner属性

获取或设置拥有此窗体的窗体。若要使某窗体归另一个窗体所有,请为其 Owner 属性分配一个对将成为所有者的窗体的引用。当一个窗体归另一窗体所有时,它便随着所有者窗体最小化和关闭。例如,如果 Form2 归窗体 Form1 所有,则关闭或最小化 Form1 时,也会关闭或最小化 Form2。并且附属窗体从不显示在其所有者窗体后面。可以将附属窗体用于查找和替换窗口之类的窗口,当选定所有者窗体时,这些窗口不应消失。若要确定某父窗体拥有的窗体,请使用OwnedForms属性。

上面是SDK帮助文档上讲的,下面我们就来使用它。

首先还是使用第一篇文章中的第二个例子,窗体如下:


说明:在这个例子中我们的两个窗体都加了一个ListBox用来显示ArrayList中的内容。

主窗体中控件:listBoxFrm1,buttonEdit;

子窗体中控件:listBoxFrm2,textBoxAdd,buttonAdd,buttonEdit,buttonOK。

主窗体中还是定义类数据成员,

private ArrayList listData1;

在构造函数里实例化它,填充数据,最后绑定到listBoxFrm1。

构造函数如下:

public Form1()

{

InitializeComponent();

this.listData1 = new ArrayList();

this.listData1.Add("Dotnet");

this.listData1.Add("C#");

this.listData1.Add("Asp.net");

this.listData1.Add("WebService");

this.listData1.Add("XML");

this.listBoxFrm1.DataSource = this.listData1;

}

主窗体的修改按钮处理函数:

private void buttonEdit_Click(object sender, System.EventArgs e)

{

Form2 formChild = new Form2();

formChild.Owner = this;

formChild.ShowDialog();

this.listBoxFrm1.DataSource = null;

this.listBoxFrm1.DataSource = this.listData1;

}

我们设置了formChild.Owner为this,这样,子窗体和主窗体就有联系了,

当然我们也可以改成如下:

private void buttonEdit_Click(object sender, System.EventArgs e)

{

Form2 formChild = new Form2();

formChild.ShowDialog(this);

this.listBoxFrm1.DataSource = null;

this.listBoxFrm1.DataSource = this.listData1;

}

不过这样还不行,目前主窗体的listData1变量外部访问不到,

private ArrayList listData1;

必须修改为public访问修饰符,

public ArrayList listData1;

也可以通过属性(property)来实现,

public ArrayList ListData1

{

get{return this.listData1;}

}

这里我采用属性,感觉语法更灵活,清楚。

下面是对Form2的修改,

构造函数又恢复原貌了。

public Form2()

{

InitializeComponent();

}

另外又新增了一个窗体的Load事件,在它的事件处理函数中来获取主窗体中的数据,

private void Form2_Load(object sender, System.EventArgs e)

{

Form1 pareForm = (Form1)this.Owner;

this.listData2 = pareForm.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)

{

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;}

/<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击