在.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]。
國內最大的酷站演示中心!