Document number【SA80SAB-SAA9SYT-SAATC-SA6UT-SA18】
总结C中窗体间传递数据的几种方法
总结C#中窗体间传递数据的几种方法 (由别人的方法整理)
在编写C#wthis指的是类Form1当前的实例。
//也可以使用函数的方法,给当前实例添加一个附属窗口 代码:(f2);
在Form2类的定义中写如下代码:
Form1 f1=;
这样f1对应的就是原来的Form1的实例了,也就可以用这个进行通讯了。但是还是要把不同类之间访问的域和函数定义成public,哎,安全确实是一个问题!!
4.使用静态类
这个也是我们经常要用到的一种数据交互方法。
下面是定义的一个类:
using System;
using ;
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 ;
using ;
using ;
using ZZ
{
public class Form1 : {
private buttonEdit;
private listBoxFrm1;
private components = null;
public Form1()
{
InitializeComponent();
= ;
}
protected override void Dispose( bool disposing )
{
if( disposing )
if(components != null)
();
( disposing );
}
[STAThread]
static void Main()
{
(new Form1());
}
private void InitializeComponent()
{
= new = new ();
= new 108);
= "buttonEdit";
= 1;
= "修改";
+= new ;
= 12;
= new 8);
= "listBoxFrm1";
= new 124);
= 2;
= new 14);
= new 141);
= "Form1";
= "Form1";
(false);
}
private void buttonEdit_Click(object sender, e)
{
Form2 formChild = new Form2();
();
= null;
= ;
}
}
}
文件
using ;
using ;
using ;
using ZZ
{
public class Form2 : {
private buttonOK;
private components = null;
private listBoxFrm2;
private buttonAdd;
private buttonDel;
private textBoxAdd;
public Form2()
{
InitializeComponent();
foreach(object o in
}
protected override void Dispose( bool di
总结C中窗体间传递数据的几种方法 来自淘豆网m.daumloan.com转载请标明出处.