2024年2月9日发(作者:)
C#窗体间的数据传值(使用静态类)
收藏
之前使用带参数的构造函数、属性以及方法实现了数据的交互,接下来的是使用静态类来完成窗体间的数据交互。这也是经常要用到的一种数据交互方法。
下面是定义的一个类:
using System;
using tions;
namespace ZZ
{
public class AppDatas
{
//静态数据成员
private static ArrayList listData;
//静态构造函数
static AppDatas()
{
listData = new ArrayList();
("DotNet");
("C#");
("");
("WebService");
("XML");
}
//静态属性
public static ArrayList ListData
{
get{return listData;}
}
//静态方法
public static ArrayList GetListData()
{
return listData;
}
}
}
上面包含了一个静态类成员,listData,一个静态构造函数static AppDatas(),用来初始化listData的数据。还有一个静态属性ListData和一个静态GetListData()方法,他们实现了同样的功能就是返回listData。
下面是完整的代码:
文件
using System;
using g;
using tions;
using entModel;
using ;
namespace ZZ
{
public class Form1 :
{
private buttonEdit;
private x listBoxFrm1;
private ner components = null;
public Form1()
{
InitializeComponent();
urce = ta;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
e();
}
}
e( disposing );
}
[STAThread]
static void Main()
{
(new Form1());
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
Edit = new ();
xFrm1 = new x();
dLayout();
//
// buttonEdit
//
on = new (128, 108);
= "buttonEdit";
ex = 1;
= "修改";
+= new
andler(Edit_Click);
//
// listBoxFrm1
//
ight = 12;
on = new (12, 8);
= "listBoxFrm1";
= new (108, 124);
ex = 2;
//
// Form1
//
aleBaseSize = new (6, 14);
Size = new (208, 141);
(xFrm1);
(Edit);
= "Form1";
= "Form1";
Layout(false);
}
#endregion
private void buttonEdit_Click(object sender, rgs e)
{
Form2 formChild = new Form2();
alog();
urce = null;
urce = ta;
}
}
}
文件
using g;
using tions;
using entModel;
using ;
namespace ZZ
{
public class Form2 :
{
private buttonOK;
private ner components = null;
private x listBoxFrm2;
private buttonAdd;
private buttonDel;
private x textBoxAdd;
public Form2()
{
InitializeComponent();
foreach(object o in ta)
(o);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
e();
}
}
e( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
OK = new ();
xFrm2 = new x();
Add = new ();
Del = new ();
xAdd = new x();
dLayout();
//
// buttonOK
//
on = new (188, 108);
= "buttonOK";
ex = 0;
= "确定";
+= new
andler(OK_Click);
//
// listBoxFrm2
//
ight = 12;
on = new (8, 8);
= "listBoxFrm2";
= new (168, 124);
ex = 2;
//
// buttonAdd
//
on = new (188, 44);
= "buttonAdd";
ex = 3;
= "增加";
+= new
andler(Add_Click);
//
// buttonDel
//
on = new (188, 76);
= "buttonDel";
ex = 4;
= "删除";
+= new
andler(Del_Click);
//
// textBoxAdd
//
on = new (188, 12);
= "textBoxAdd";
= new (76, 21);
ex = 5;
= "";
//
// Form2
//
aleBaseSize = new (6, 14);
Size = new (272, 141);
(xAdd);
(Del);
(Add);
(xFrm2);
(OK);
= "Form2";
= "Form2";
Layout(false);
}
#endregion
private void buttonOK_Click(object sender, rgs e)
{
();
}
private void buttonAdd_Click(object sender, rgs e)
{
if(().Length>0)
{
(());
(());
}
else
("请输入添加的内容!");
}
private void buttonDel_Click(object sender, rgs e)
{
int index = edIndex;
if(index!=-1)
{
At(index);
At(index);
}
else
("请选择删除项!");
}
}
}
调试可以看到实现了同样的功能。
总结,笔者认为使用静态类比较多的地方就是把应用程序的配置文件装载到一个静态类里面,让所有的窗体和其他实例都可以通过静态属性以及静态方法使用这些数据,比如三层结构或多层结构都可以访问它,而不是在多个实例间传来传去。在这里我们讨论的是Windows窗体,其实在两个不同的实例间交互数据,都可以采用文章中的方案实现,除非是这个类特有的属性或着方法。


发布评论