C#窗体间的数据传值(使用静态类)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C#窗体间的数据传值(使用静态类) 收藏

之前使用带参数的构造函数、属性以及方法实现了数据的交互,接下来的是使用静态类来完成窗体间的数据交互。这也是经常要用到的一种数据交互方法。下面是定义的一个类:

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

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

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 ponentModel.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());

}

#region Windows 窗体设计器生成的代码

private void InitializeComponent()

{

this.buttonEdit = new System.Windows.Forms.Button();

this.listBoxFrm1 = new System.Windows.Forms.ListBox(); this.SuspendLayout();

//

// buttonEdit

//

this.buttonEdit.Location = new System.Drawing.Point(128, 108); = "buttonEdit";

this.buttonEdit.TabIndex = 1;

this.buttonEdit.Text = "修改";

this.buttonEdit.Click += new

System.EventHandler(this.buttonEdit_Click);

//

// listBoxFrm1

//

this.listBoxFrm1.ItemHeight = 12;

this.listBoxFrm1.Location = new System.Drawing.Point(12, 8); = "listBoxFrm1";

this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);

this.listBoxFrm1.TabIndex = 2;

//

// Form1

//

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

= "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

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

相关文档
最新文档