




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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#);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()方法,他们实现了同样的功能就是返回listData。下面是完整的代码: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 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()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);this.buttonEdit.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;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);this.Name = Form1;this.Text = 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.Collections;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.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 );#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.Forms.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.buttonOK_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.Drawing.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.TabIndex = 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;this.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 = Form2;this.Text = Form2;this.ResumeLayout(false);#endregionprivate 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();elseMessageBox.Show(请输入添加的内容!);private void buttonDel_Click(object sender, System.EventArgs e)int index = this.listBoxFrm2.SelectedIndex;if(index!=-1) AppDatas.L
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 计算机组成原理 课件 4 指令系统
- 巡视巡察培训课件
- 巡察工作培训课件
- 岩土检测员岗位培训课件
- 民用建筑抗震加固工程造价合同
- 知名人士个人肖像权授权使用及收益分成协议
- 白酒代理业务合作及市场渠道建设合同
- 智能家居产品出样体验合作协议
- 高科技企业股东持股比例调整协议合同
- 酒店业2025年连锁酒店运营风险分析与特色服务质量打造报告
- 【2025年】郴州社区专职工作人员招聘考试笔试试卷【附答案】
- 2025发展对象考试题库附含答案
- 2025广东广州市越秀区大东街道办事处经济发展办招聘辅助人员(统计员岗)1人笔试备考试题及答案解析
- 2025年骨科颈椎间盘突出症保守治疗要点考试卷答案及解析
- 2025国新控股(上海)有限公司总经理招聘1人笔试参考题库附答案解析
- 2025国资国企穿透式监管白皮书
- 医院查房制度培训课件
- 医学规培读书报告
- 2025年法考主观试题库及答案
- DB31∕T 1543-2025 快速公交(BRT)支持自动驾驶的车路协同架构与技术要求
- 小学数学北师大版四年级上二、线与角-线的认识练习(含答案)
评论
0/150
提交评论