分享
 
 
 

.Net中掌握Windows窗体间数据交互(3)

王朝c#·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

在第一篇和第二篇文章中我们使用带参数的构造函数、属性以及方法实现了数据的交互,接下来要讲的是使用静态类来完成窗体间的数据交互。这个也是我们经常要用到的一种数据交互方法。

三.使用静态类

下面是定义的一个类:

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().Length0)

{

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("请选择删除项!");

}

}

}

总结,

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有