1、总结C#中窗体间传递数据的几种方法总结C#中窗体间传递数据的几种方法 (由别人的方法整理) 在编写C#windows应用程序的时候我们经常会遇到这种问题,怎么样在两个窗体间传递数据呢?以下是我整理的网上的各种方法,在遇到一个实际问题:在form1中打开一个form2窗口作为录入界面,将录入的值经转换后在form1中显示。 采用了委托的方法,可以实现。(与VC的回调的应用相似)1可以通过委托的方法来解决问题:通过form1做一个录入界面,将里边通过文本框录入的数值复值给 form2中的listview各列,用3个textbox1.text举例吧,分别对应listview的3个列。可以这么做,如果
2、两个窗体是在同一个命名空间下定义一个代理,注意这个代理是全局的:(即同一命名空间下,与Form1,Form2平级的)public delegate void MyInvoke(string Item1,string Item2,string Item3);/在窗体From2中有这么一个回调函数,用于在ListView里添加一个新项的:private void UpdateListView(string Item1,string Item2,string Item3) ListView1.Items.Add(Item1); ListView1.ItemsListView1.Items.Count
3、 - 1.SubItems.Add(Item2); ListView1.ItemsListView1.Items.Count - 1.SubItems.Add(Item3);/比如说点击Form2的一个按钮弹出Form1进行录入,在点击按钮的事件下:/把委托传过去Form1 frmEdit=new Form1(new MyInvoke(UpdateListView);frmEdit.ShowDialog(this);/在Form1里定义一个属性private MyInvoke mi=null;在构造函数中接收这个委托:public Form1(MyInvoke myInvoke) this.m
4、i=myInvoke;/录入数据后,点击OK按钮,在点击事件下:/回调this.mi(this.TextBox1.Text,this.TextBox3.Text,this.TextBox3.Text);this.Close();/关闭Form1补充:如果我要是想再把form2的值给form1,Form1 frmEdit=new Form1(new MyInvoke(UpdateListView),string para1,string para2.);frmEdit.ShowDialog(this);然后将Form1的构造函数改成可以接收几个参数的就行了。2假如主框架为Form1,打开的搜索对
5、话框是Form2.直接在Form2类中申明一个Form1实例:Form1 f1=new Form1();然后就可以通过f1来调用Form1中的域和函数了。其实不是这样的,你申明的新的Form1实例不是原来的那个Form1对象了,这样操作的是新的Form1中的域和函数,和最先打开的Form1是没有关系的。我们要做的是把当前的Form1实例传递给Form2,如果是这样的话,问题就很好解决了。 方法1:首先,我们在Form2中定义: private Form1 mF_Form 我们更改Form2的构造函数为有参数的 public Form2 ( Form1 myForm ) / / Windows
6、窗体设计器支持所必需的 / InitializeComponent ( ) ; this.mF_Form = myForm ; /这样在Form1中申明Form2的时候就把Form1的实例传递过来了 / / TODO: 在 InitializeComponent 调用后添加任何构造函数代码 / 在Form1中,我在 要用到Form2的地方申明如下: Form2 f2=new Form2(this);/这里的this指的就是Form1当前的实例,也就是把当前Form1的实例通过Form2的构造函数传递给Form2类(其实在网上看到过比较蠢的方式,就是在构造函数里面传递要传递的信息如:字符串或是数
7、字等,这样做很有局限性,不能传递其他的,所有我们可以直接传递实例,来完成传递更多的信息。) 这样在Form2中使用myForm 就可以对原来的Form1窗口进行操作了。但是你要把要操作的Form1中的域和函数定义成public形式的(这样可能不安全),此时的myForm就是真正的最开始打开的Form1了,你可以用这个实例来进行两个窗体的通讯了。 ()3其实C#中提供了窗体间进行通讯的现成的属性,呵呵,我们能想到的,微软也想到了,他们创造的语言其实确实可以说是人性化了。 在Form1类中申明Form2时用如下代码: Form2 f2=new Form2();/类Form2中的构造函数不改,还是无
8、参的 f2.owner=this;/这里的this指的是类Form1当前的实例。 /也可以使用函数的方法,给当前实例添加一个附属窗口 代码:this.AddOwnedForm(f2); 在Form2类的定义中写如下代码: Form1 f1=this.owner; 这样f1对应的就是原来的Form1的实例了,也就可以用这个进行通讯了。但是还是要把不同类之间访问的域和函数定义成public,哎,安全确实是一个问题! 4使用静态类 这个也是我们经常要用到的一种数据交互方法。下面是定义的一个类:using System;using System.Collections;namespace ZZ pub
9、lic 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 ListData getreturn listData; public static ArrayList GetListData() return
10、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
11、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; protec
12、ted 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
13、 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.
14、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.S
15、ize(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
16、= 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.ComponentMo
17、del.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.ListDat
18、a) 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.Form
19、s.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.T
20、abIndex = 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); thi
21、s.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(1
22、88, 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.D
23、rawing.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.Con
24、trols.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)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1