《C#窗体间的数据传值(使用静态类)》由会员分享,可在线阅读,更多相关《C#窗体间的数据传值(使用静态类)(7页珍藏版)》请在金锄头文库上搜索。
1、 C#窗体间的数据传值(使用静态类) 收藏之前使用带参数的构造函数、属性以及方法实现了数据的交互,接下来的是使用静态类来完成窗体间的数据交互。这也是经常要用到的一种数据交互方法。下面是定义的一个类:using System;using System.Collections;namespace ZZpublic class AppDatas/静态数据成员private static ArrayList listData;/静态构造函数static AppDatas()listData = new ArrayList();listData.Add(DotNet);listData.Add(C#);
2、listData.Add(A);listData.Add(WebService);listData.Add(XML);/静态属性public static ArrayList ListDatagetreturn listData;/静态方法public static ArrayList GetListData()return listData;上面包含了一个静态类成员,listData,一个静态构造函数 static AppDatas(),用来初始化 listData 的数据。还有一个静态属性 ListData 和一个静态 GetListData()方法,他们实现了同样的功能就是返回 list
3、Data。下面是完整的代码:Form1.cs 文件using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace ZZpublic class Form1 : System.Windows.Forms.Formprivate System.Windows.Forms.Button buttonEdit;private System.Windows.Forms.ListBox listBoxFrm1;private
4、 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 );STAThreadstatic void Main() Appl
5、ication.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);this.butto
6、nEdit.Name = 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);this.listBoxFrm1.Name = listBoxFrm1;thi
7、s.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);this.Name = Form1;this.Te
8、xt = Form1;this.ResumeLayout(false);#endregionprivate 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.Collectio
9、ns;using System.ComponentModel;using System.Windows.Forms;namespace ZZpublic class Form2 : System.Windows.Forms.Formprivate System.Windows.Forms.Button buttonOK;private System.ComponentModel.Container components = null;private System.Windows.Forms.ListBox listBoxFrm2;private System.Windows.Forms.But
10、ton 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)co
11、mponents.Dispose();base.Dispose( disposing );#region Windows 窗体设计器生成的代码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.Form
12、s.Button();this.textBoxAdd = new System.Windows.Forms.TextBox(); this.SuspendLayout();/ / buttonOK/ 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.buttonO
13、K_Click);/ / listBoxFrm2/ 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;/ / buttonAdd/ this.buttonAdd.Location = new System.Drawin
14、g.Point(188, 44);this.buttonAdd.Name = buttonAdd;this.buttonAdd.TabIndex = 3;this.buttonAdd.Text = 增加;this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);/ / buttonDel/ this.buttonDel.Location = new System.Drawing.Point(188, 76);this.buttonDel.Name = buttonDel;this.buttonDel.TabInd
15、ex = 4;this.buttonDel.Text = 删除;this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click);/ / textBoxAdd/ 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;thi
16、s.textBoxAdd.Text = ; / / Form2/ 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 = F