您的位置:首页 > 编程语言 > C#

总结C#中窗体间传递数据的几种方法

2009-09-10 14:15 531 查看
C#窗体间数据传递的四种方法


(1)将需要接收数据的窗体声明为全局静态变量,如声明为Program类的静态成员,这样就可以在传递数据的窗体中访问接收数据的窗体。

(2)通过共同访问数据库实现窗体间数据共享。

(3)设置窗体的Owner属性,可以建立窗体间所属关系,窗体间就可以互相访问。

(4)在发送数据的窗体中声明有返回类型的方法,或通过属性来将数据传递给接收数据的窗体。

在编写C#windows应用程序的时候我们经常会遇到这种问题,怎么样在两个窗体间传递数据呢?以下是我整理的网上的各种方法,在遇到一个实际问题:在form1中打开一个form2窗口作为录入界面,将录入的值经转换后在form1中显示。 采用了委托的方法,可以实现。(与VC的回调的应用相似)
1.可以通过委托的方法来解决
问题:通过form1做一个录入界面,将里边通过文本框录入的数值复值给 form2中的listview各列,用3个textbox1.text举例吧,分别对应listview的3个列。
可以这么做,如果两个窗体是在同一个命名空间下
定义一个代理,注意这个代理是全局的:(即同一命名空间下,与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.Items[ListView1.Items.Count - 1].SubItems.Add(Item2);
ListView1.Items[ListView1.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.mi=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,打开的搜索对话框是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 窗体设计器支持所必需的
//
InitializeComponent ( ) ;
this.mF_Form = myForm ; /////这样在Form1中申明Form2的时候就把Form1的实例传递过来了
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
在Form1中,我在 要用到Form2的地方申明如下:
Form2 f2=new Form2(this);////这里的this指的就是Form1当前的实例,也就是把当前Form1的实例通过Form2的构造函数传递给Form2类(其实在网上看到过比较蠢的方式,就是在构造函数里面传递要传递的信息如:字符串或是数字等,这样做很有局限性,不能传递其他的,所有我们可以直接传递实例,来完成传递更多的信息。)
这样在Form2中使用myForm 就可以对原来的Form1窗口进行操作了。但是你要把要操作的Form1中的域和函数定义成public形式的(这样可能不安全),此时的myForm就是真正的最开始打开的Form1了,你可以用这个实例来进行两个窗体的通讯了。 ()
3.其实C#中提供了窗体间进行通讯的现成的属性,呵呵,我们能想到的,微软也想到了,他们创造的语言其实确实可以说是人性化了。
在Form1类中申明Form2时用如下代码:
Form2 f2=new Form2();//////类Form2中的构造函数不改,还是无参的
f2.owner=this;////这里的this指的是类Form1当前的实例。
//也可以使用函数的方法,给当前实例添加一个附属窗口 代码:this.AddOwnedForm(f2);
在Form2类的定义中写如下代码:
Form1 f1=this.owner;
这样f1对应的就是原来的Form1的实例了,也就可以用这个进行通讯了。但是还是要把不同类之间访问的域和函数定义成public,哎,安全确实是一个问题!!

4.使用静态类
这个也是我们经常要用到的一种数据交互方法。
下面是定义的一个类:
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().Length>0)
{
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("请选择删除项!");
}
}
}
总结,我认为使用静态类比较多的地方就是把应用程序的配置文件装载到一个静态类里面,让所有的窗体和其他实例都可以通过静态属性以及静态方法使用这些数据,比如三层结构或多层结构都可以访问它,而不是在多个实例间传来传去。在这里我们讨论的是Windows窗体,其实在两个不同的实例间交互数据,都可以采用三篇文章中的方案实现,除非是这个类特有的属性或着方法。现在都讲完了,虽然不是什么高深的东西,但是希望能对一些初学者有所帮助,同时也欢迎各位朋友进行技术交流,共同提高。

分析上面几种方法:
1. 采用了委托的方法,可以实现。:很好的实现了数据处理与数据显示的分离,即FORM2(主)显示与FORM1数据处理,(不需要将FORM2的显示放在FORM1中)与VC的回调的应用有延续性。并且确保了FORM1中要修改的属性的私有性。

2. 方法2、3都是传递主窗口的引用,比较简单易用。可以实现FORM2(主)与FORM1所有数据的传递(不过需要将要FORM1传递和要修改的数据设为PUBLIC),而这样会存在安全问题。

委托方法可以很好地实现数据的保护

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: