首页 > 编程 > .NET > 正文

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

2024-07-10 13:00:21
字体:
来源:转载
供稿:网友


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

zhzuo(秋枫)

在第一篇和第二篇文章中我们使用带参数的构造函数、属性以及方法实现了数据的交互,接下来要讲的是使用静态类来完成窗体间的数据交互。这个也是我们经常要用到的一种数据交互方法。

三.使用静态类

下面是定义的一个类:

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

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窗体,其实在两个不同的实例间交互数据,都可以采用三篇文章中的方案实现,除非是这个类特有的属性或着方法。现在都讲完了,虽然不是什么高深的东西,但是希望能对一些初学者有所帮助,要是能真正的能解决一些朋友的实际问题,也算是我没有浪费时间来写文章,同时也欢迎各位朋友进行技术交流,共同提高,我的邮件地址[email protected]。
国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表