总结C#中窗体间传递数据的几种方法_第1页
总结C#中窗体间传递数据的几种方法_第2页
总结C#中窗体间传递数据的几种方法_第3页
总结C#中窗体间传递数据的几种方法_第4页
总结C#中窗体间传递数据的几种方法_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、总结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);   

3、 ListView1.ItemsListView1.Items.Count - 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 M

4、yInvoke 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 pa

5、ra1,string para2.);frmEdit.ShowDialog(this);然后将Form1的构造函数改成可以接收几个参数的就行了。 2假如主框架为Form1,打开的搜索对话框是Form2.直接在Form2类中申明一个Form1实例:Form1 f1=new Form1();然后就可以通过f1来调用Form1中的域和函数了。其实不是这样的,你申明的新的Form1实例不是原来的那个Form1对象了,这样操作的是新的Form1中的域和函数,和最先打开的Form1是没有关系的。我们要做的是把当前的Form1实例传递给Form2,如果是这样的话,问题就很好解决了。 方法1:首先,

6、我们在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=

7、new  Form2(this);/这里的this指的就是Form1当前的实例,也就是把当前Form1的实例通过Form2的构造函数传递给Form2类(其实在网上看到过比较蠢的方式,就是在构造函数里面传递要传递的信息如:字符串或是数字等,这样做很有局限性,不能传递其他的,所有我们可以直接传递实例,来完成传递更多的信息。) 这样在Form2中使用myForm 就可以对原来的Form1窗口进行操作了。但是你要把要操作的Form1中的域和函数定义成public形式的(这样可能不安全),此时的myForm就是真正的最开始打开的Form1了,你可以用这个实例来进行两个窗体的通讯了。 ()3其实

8、C#中提供了窗体间进行通讯的现成的属性,呵呵,我们能想到的,微软也想到了,他们创造的语言其实确实可以说是人性化了。 在Form1类中申明Form2时用如下代码: Form2 f2=new Form2();/类Form2中的构造函数不改,还是无参的 f2.owner=this;/这里的this指的是类Form1当前的实例。 /也可以使用函数的方法,给当前实例添加一个附属窗口  代码:this.AddOwnedForm(f2); 在Form2类的定义中写如下代码: Form1 f1=this.owner; 这样f1对应的就是原来的Form1的实例了,也就可以用这个进行通讯了。但是还是要把

9、不同类之间访问的域和函数定义成public,哎,安全确实是一个问题!    4使用静态类    这个也是我们经常要用到的一种数据交互方法。下面是定义的一个类:using System;using System.Collections;namespace ZZ     public class AppDatas              private static ArrayList l

10、istData;         static AppDatas()                       listData = new ArrayList();            

11、;  listData.Add("DotNet");              listData.Add("C#");              listData.Add("A");       &#

12、160;      listData.Add("WebService");              listData.Add("XML");                  public static ArrayLi

13、st ListData                       getreturn listData;                  public static ArrayList GetListData() &

14、#160;                     return listData;              上面包含了一个静态类成员,listData,一个静态构造函数static AppDatas(),用来初始化listData的数据。还有一个静态属性ListData和一个静态Get

15、ListData()方法,他们实现了同样的功能就是返回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     &

16、#160;        private System.Windows.Forms.Button buttonEdit;         private System.Windows.Forms.ListBox listBoxFrm1;         private System.ComponentModel.Container components = null;

17、         public Form1()                       InitializeComponent();              this.lis

18、tBoxFrm1.DataSource = AppDatas.ListData;                                protected override void Dispose( bool disposing )     

19、0;                 if( disposing )                   if(components != null)          

20、             components.Dispose();              base.Dispose( disposing );                  STA

21、Thread         static void Main()                        Application.Run(new Form1();            

22、60;     private void InitializeComponent()                       this.buttonEdit = new System.Windows.Forms.Button();         

23、0;    this.listBoxFrm1 = new System.Windows.Forms.ListBox();              this.SuspendLayout();              this.buttonEdit.Location = new System.Drawing.

24、Point(128, 108);              this.buttonEdit.Name = "buttonEdit"              this.buttonEdit.TabIndex = 1;        

25、0;     this.buttonEdit.Text = "修改"              this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);              this.l

26、istBoxFrm1.ItemHeight = 12;              this.listBoxFrm1.Location = new System.Drawing.Point(12, 8);              this.listBoxFrm1.Name = "listBoxFrm1" &#

27、160;            this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);              this.listBoxFrm1.TabIndex = 2;          &#

28、160;   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);              this.ClientSize = new System.Drawing.Size(208, 141);              this.Controls.Add

29、(this.listBoxFrm1);              this.Controls.Add(this.buttonEdit);              this.Name = "Form1"          

30、;    this.Text = "Form1"              this.ResumeLayout(false);                  private void buttonEdit_Click(object sender, Syst

31、em.EventArgs e)                       Form2 formChild = new Form2();              formChild.ShowDialog();    &

32、#160;         this.listBoxFrm1.DataSource = null;              this.listBoxFrm1.DataSource = AppDatas.ListData;               Form

33、2.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

34、buttonOK;         private System.ComponentModel.Container components = null;         private System.Windows.Forms.ListBox listBoxFrm2;         private System.Windows.Forms.Button b

35、uttonAdd;         private System.Windows.Forms.Button buttonDel;         private System.Windows.Forms.TextBox textBoxAdd;         public Form2()      

36、                 InitializeComponent();              foreach(object o in AppDatas.ListData)           

37、60;       this.listBoxFrm2.Items.Add(o);                  protected override void Dispose( bool disposing )              

38、0;        if( disposing )                   if(components != null)                  &

39、#160;    components.Dispose();              base.Dispose( disposing );                  private void InitializeComponent()   

40、                    this.buttonOK = new System.Windows.Forms.Button();              this.listBoxFrm2 = new System.Windows.Forms.ListBox(); &

41、#160;            this.buttonAdd = new System.Windows.Forms.Button();              this.buttonDel = new System.Windows.Forms.Button();        

42、;      this.textBoxAdd = new System.Windows.Forms.TextBox();              this.SuspendLayout();              this.buttonOK.Location = new System.

43、Drawing.Point(188, 108);              this.buttonOK.Name = "buttonOK"              this.buttonOK.TabIndex = 0;        &#

44、160;     this.buttonOK.Text = "确定"              this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);              this.listB

45、oxFrm2.ItemHeight = 12;              this.listBoxFrm2.Location = new System.Drawing.Point(8, 8);              this.listBoxFrm2.Name = "listBoxFrm2"  &

46、#160;           this.listBoxFrm2.Size = new System.Drawing.Size(168, 124);              this.listBoxFrm2.TabIndex = 2;           &

47、#160;  this.buttonAdd.Location = new System.Drawing.Point(188, 44);              this.buttonAdd.Name = "buttonAdd"              this.buttonAdd.TabIndex = 3

48、;              this.buttonAdd.Text = "增加"              this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);      

49、;        this.buttonDel.Location = new System.Drawing.Point(188, 76);              this.buttonDel.Name = "buttonDel"            &#

50、160; this.buttonDel.TabIndex = 4;              this.buttonDel.Text = "删除"              this.buttonDel.Click += new System.EventHandler(this.buttonDel_Click);

51、60;             this.textBoxAdd.Location = new System.Drawing.Point(188, 12);              this.textBoxAdd.Name = "textBoxAdd"      

52、60;       this.textBoxAdd.Size = new System.Drawing.Size(76, 21);              this.textBoxAdd.TabIndex = 5;              this.textBoxAdd.Te

53、xt = ""              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);              this.ClientSize = new System.Drawing.Size(272, 141);   

54、;           this.Controls.Add(this.textBoxAdd);              this.Controls.Add(this.buttonDel);              this.Contro

55、ls.Add(this.buttonAdd);              this.Controls.Add(this.listBoxFrm2);              this.Controls.Add(this.buttonOK);        &#

56、160;     this.Name = "Form2"              this.Text = "Form2"              this.ResumeLayout(false);     

57、             private void buttonOK_Click(object sender, System.EventArgs e)                       this.Close();     

58、;             private void buttonAdd_Click(object sender, System.EventArgs e)                       if(this.textBoxAdd.Text.Trim().Length>

59、;0)                                 AppDatas.ListData.Add(this.textBoxAdd.Text.Trim();          

60、;         this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim();                            else     

61、60;             MessageBox.Show("请输入添加的内容!");                                private

62、void buttonDel_Click(object sender, System.EventArgs e)                       int index = this.listBoxFrm2.SelectedIndex;            &#

63、160; if(index!=-1)                                  AppDatas.ListData.RemoveAt(index);         

64、          this.listBoxFrm2.Items.RemoveAt(index);                            else       &#

65、160;           MessageBox.Show("请选择删除项!");                       总结,我认为使用静态类比较多的地方就是把应用程序的配置文件装载到一个静态类里面,让所有的窗体和其他实例都可以通过静态属性以及静态方法使用这些数据,比如三层结构或

66、多层结构都可以访问它,而不是在多个实例间传来传去。在这里我们讨论的是Windows窗体,其实在两个不同的实例间交互数据,都可以采用三篇文章中的方案实现,除非是这个类特有的属性或着方法。现在都讲完了,虽然不是什么高深的东西,但是希望能对一些初学者有所帮助,同时也欢迎各位朋友进行技术交流,共同提高。  分析上面几种方法:1  采用了委托的方法,可以实现。:很好的实现了数据处理与数据显示的分离,即FORM2(主)显示与FORM1数据处理,(不需要将FORM2的显示放在FORM1中)与VC的回调的应用有延续性。并且确保了FORM1中要修改的属性的私有性。 2&#

67、160; 方法2、3都是传递主窗口的引用,比较简单易用。可以实现FORM2(主)与FORM1所有数据的传递(不过需要将要FORM1传递和要修改的数据设为PUBLIC),而这样会存在安全问题。 委托方法可以很好地实现数据的保护 总结C#中窗体间传递数据的几种方法 (由别人的方法整理)   在编写C#windows应用程序的时候我们经常会遇到这种问题,怎么样在两个窗体间传递数据呢?以下是我整理的网上的各种方法,在遇到一个实际问题:在form1中打开一个form2窗口作为录入界面,将录入的值经转换后在form1中显示。 采用了委托的方法,可以实现。(与VC的回

68、调的应用相似)1可以通过委托的方法来解决问题:通过form1做一个录入界面,将里边通过文本框录入的数值复值给 form2中的listview各列,用3个textbox1.text举例吧,分别对应listview的3个列。可以这么做,如果两个窗体是在同一个命名空间下定义一个代理,注意这个代理是全局的:(即同一命名空间下,与Form1,Form2平级的)public delegate void MyInvoke(string Item1,string Item2,string Item3); /在窗体From2中有这么一个回调函数,用于在ListView里添加一个新项的:private

69、void UpdateListView(string Item1,string Item2,string Item3)    ListView1.Items.Add(Item1);    ListView1.ItemsListView1.Items.Count - 1.SubItems.Add(Item2);    ListView1.ItemsListView1.Items.Count - 1.SubItems.Add(Item3); /比如说点击Form2的一个按钮弹出Form1进行录入,在

70、点击按钮的事件下:/把委托传过去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.

71、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中的域和函数了。其实不

72、是这样的,你申明的新的Form1实例不是原来的那个Form1对象了,这样操作的是新的Form1中的域和函数,和最先打开的Form1是没有关系的。我们要做的是把当前的Form1实例传递给Form2,如果是这样的话,问题就很好解决了。 方法1:首先,我们在Form2中定义: private Form1 mF_Form 我们更改Form2的构造函数为有参数的 public Form2 ( Form1 myForm ) / / Windows 窗体设计器支持所必需的 / InitializeComponent ( ) ; this.mF_Form  = myForm ;  

73、; /这样在Form1中申明Form2的时候就把Form1的实例传递过来了 / / TODO: 在 InitializeComponent 调用后添加任何构造函数代码 / 在Form1中,我在 要用到Form2的地方申明如下: Form2 f2=new  Form2(this);/这里的this指的就是Form1当前的实例,也就是把当前Form1的实例通过Form2的构造函数传递给Form2类(其实在网上看到过比较蠢的方式,就是在构造函数里面传递要传递的信息如:字符串或是数字等,这样做很有局限性,不能传递其他的,所有我们可以直接传递实例,来完成传递更多的信息。) 这样在Form2中使

74、用myForm 就可以对原来的Form1窗口进行操作了。但是你要把要操作的Form1中的域和函数定义成public形式的(这样可能不安全),此时的myForm就是真正的最开始打开的Form1了,你可以用这个实例来进行两个窗体的通讯了。 ()3其实C#中提供了窗体间进行通讯的现成的属性,呵呵,我们能想到的,微软也想到了,他们创造的语言其实确实可以说是人性化了。 在Form1类中申明Form2时用如下代码: Form2 f2=new Form2();/类Form2中的构造函数不改,还是无参的 f2.owner=this;/这里的this指的是类Form1当前的实例。 /也可以使用函数的方法,给当前

75、实例添加一个附属窗口  代码:this.AddOwnedForm(f2); 在Form2类的定义中写如下代码: Form1 f1=this.owner; 这样f1对应的就是原来的Form1的实例了,也就可以用这个进行通讯了。但是还是要把不同类之间访问的域和函数定义成public,哎,安全确实是一个问题!    4使用静态类    这个也是我们经常要用到的一种数据交互方法。下面是定义的一个类:using System;using System.Collections;namespace ZZ    

76、; public class AppDatas              private static ArrayList listData;         static AppDatas()                  

77、     listData = new ArrayList();              listData.Add("DotNet");              listData.Add("C#");    

78、0;         listData.Add("A");              listData.Add("WebService");              listData.Add("XML&quo

79、t;);                  public static ArrayList ListData                       getreturn listData;   &

80、#160;              public static ArrayList GetListData()                       return listData;      

81、0;       上面包含了一个静态类成员,listData,一个静态构造函数static AppDatas(),用来初始化listData的数据。还有一个静态属性ListData和一个静态GetListData()方法,他们实现了同样的功能就是返回listData。由于前面两篇文章已经讲了很多,这里不细说了,下面是完整的代码:Form1.cs文件using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.

82、Windows.Forms;namespace ZZ     public class Form1 : System.Windows.Forms.Form              private System.Windows.Forms.Button buttonEdit;         private System.Windows.Forms.List

83、Box listBoxFrm1;         private System.ComponentModel.Container components = null;         public Form1()                    

84、;   InitializeComponent();              this.listBoxFrm1.DataSource = AppDatas.ListData;                        

85、60;       protected override void Dispose( bool disposing )                       if( disposing )           &#

86、160;       if(components != null)                       components.Dispose();              base

87、.Dispose( disposing );                  STAThread         static void Main()                   

88、     Application.Run(new Form1();                  private void InitializeComponent()                    

89、   this.buttonEdit = new System.Windows.Forms.Button();              this.listBoxFrm1 = new System.Windows.Forms.ListBox();              this.SuspendLayout();&#

90、160;             this.buttonEdit.Location = new System.Drawing.Point(128, 108);              this.buttonEdit.Name = "buttonEdit"      &

91、#160;       this.buttonEdit.TabIndex = 1;              this.buttonEdit.Text = "修改"              this.buttonEdit.Click += new Syste

92、m.EventHandler(this.buttonEdit_Click);              this.listBoxFrm1.ItemHeight = 12;              this.listBoxFrm1.Location = new System.Drawing.Point(12, 8);  

93、;            this.listBoxFrm1.Name = "listBoxFrm1"              this.listBoxFrm1.Size = new System.Drawing.Size(108, 124);        

94、      this.listBoxFrm1.TabIndex = 2;              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);              this.ClientSize = new Sys

95、tem.Drawing.Size(208, 141);              this.Controls.Add(this.listBoxFrm1);              this.Controls.Add(this.buttonEdit);       &#

96、160;      this.Name = "Form1"              this.Text = "Form1"              this.ResumeLayout(false);    

97、              private void buttonEdit_Click(object sender, System.EventArgs e)                       Form2 formChild = new Form2();

98、0;             formChild.ShowDialog();              this.listBoxFrm1.DataSource = null;              this.list

99、BoxFrm1.DataSource = AppDatas.ListData;               Form2.cs文件using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;namespace ZZ     public class Form2 : System.Windo

100、ws.Forms.Form              private System.Windows.Forms.Button buttonOK;         private System.ComponentModel.Container components = null;         private Syst

101、em.Windows.Forms.ListBox listBoxFrm2;         private System.Windows.Forms.Button buttonAdd;         private System.Windows.Forms.Button buttonDel;         private System.Windows.F

102、orms.TextBox textBoxAdd;         public Form2()                       InitializeComponent();          

103、60;   foreach(object o in AppDatas.ListData)                   this.listBoxFrm2.Items.Add(o);                  protected

104、 override void Dispose( bool disposing )                       if( disposing )                   if(compo

105、nents != null)                       components.Dispose();              base.Dispose( disposing );    

106、60;             private void InitializeComponent()                       this.buttonOK = new System.Windows.Forms.Button();   

107、;           this.listBoxFrm2 = new System.Windows.Forms.ListBox();              this.buttonAdd = new System.Windows.Forms.Button();         

108、     this.buttonDel = new System.Windows.Forms.Button();              this.textBoxAdd = new System.Windows.Forms.TextBox();              this.SuspendL

109、ayout();              this.buttonOK.Location = new System.Drawing.Point(188, 108);              this.buttonOK.Name = "buttonOK"     

110、60;        this.buttonOK.TabIndex = 0;              this.buttonOK.Text = "确定"              this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);              this.listBoxFrm2.ItemHeight = 12;   

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论