C#实现窗体间的通信

以下将窗体间的几种通信实现方式做一下罗列:首先新建一个窗体Form1,在其中放置一个Textbox、Button控件。再新建一个窗体Form2,其上放置一个Button控件。具体代码示例如下:

//Form1.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace WindowsFormsApplication2
11 {
12     //03通过接口类进行通讯
13     //public partial class Form1 : Form, Interface1
14     public partial class Form1 : Form
15     {
16         public Form1()
17         {
18             InitializeComponent();
19         }
20 
21         private void button1_Click(object sender, EventArgs e)
22         {
23             //00借助公共类在窗体间传递数据
24             //Class1.str = textBox1.Text;
25             //Form2 frm2 = new Form2();
26             //frm2.Show();
27 
28             //01通过传窗口引用进行数据传递 
29             //Form2 frm2 = new Form2(this);
30             //frm2.Show();
31 
32             //02通过传窗口引用进行数据传递
33             //Form2 frm2 = new Form2(this.textBox1);
34             //frm2.Show();
35 
36             //03通过接口类进行通讯
37             //Form2 frm2 = new Form2(this);
38             //frm2.Show();
39 
40             //04通过委托实现窗体间通信
41             //Form2 frm2 = new Form2();
42             //frm2.TitleChanged = new Form2.TitleChangedHandler(TitleChanged);
43             //frm2.Show();
44 
45             //05通过自定义事件、事件参数进行窗体通信
46             Form2 frm2 = new Form2();
47             frm2.TitleChanged +=new Form2.TitleChangedEventHandler(FrmTitleChanged);
48             frm2.Show();
49         }
50         
51          //03通过接口类进行通讯
52         //public void ChangeTitle(String sTitle)
53         //{ 
54         //    this.Text = sTitle;        
55         //}
56 
57         //04通过委托实现窗体间通信
58         //protected void TitleChanged(String sTitle)
59         //{
60         //    this.Text = sTitle;
61         //}
62 
63         //05通过自定义事件、事件参数进行窗体通信
64         public void FrmTitleChanged(object sender, Form2.TitleChangedEventArgs e)
65         {
66             this.Text = e.Title;
67         }
68 
69 
70     }
71 }
View Code

 //Form1.Designer.cs

 1 namespace WindowsFormsApplication2
 2 {
 3     partial class Form1
 4     {
 5         /// <summary>
 6         /// 必需的设计器变量。
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// 清理所有正在使用的资源。
12         /// </summary>
13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows 窗体设计器生成的代码
24 
25         /// <summary>
26         /// 设计器支持所需的方法 - 不要
27         /// 使用代码编辑器修改此方法的内容。
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.label1 = new System.Windows.Forms.Label();
33             this.textBox1 = new System.Windows.Forms.TextBox();
34             this.SuspendLayout();
35             // 
36             // button1
37             // 
38             this.button1.Location = new System.Drawing.Point(14, 179);
39             this.button1.Name = "button1";
40             this.button1.Size = new System.Drawing.Size(75, 30);
41             this.button1.TabIndex = 0;
42             this.button1.Text = "打开子窗口";
43             this.button1.UseVisualStyleBackColor = true;
44             this.button1.Click += new System.EventHandler(this.button1_Click);
45             // 
46             // label1
47             // 
48             this.label1.AutoSize = true;
49             this.label1.Location = new System.Drawing.Point(12, 93);
50             this.label1.Name = "label1";
51             this.label1.Size = new System.Drawing.Size(125, 12);
52             this.label1.TabIndex = 1;
53             this.label1.Text = "设置子窗口的标题为:";
54             // 
55             // textBox1
56             // 
57             this.textBox1.Location = new System.Drawing.Point(14, 117);
58             this.textBox1.Name = "textBox1";
59             this.textBox1.Size = new System.Drawing.Size(228, 21);
60             this.textBox1.TabIndex = 2;
61             // 
62             // Form1
63             // 
64             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
65             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
66             this.ClientSize = new System.Drawing.Size(292, 266);
67             this.Controls.Add(this.textBox1);
68             this.Controls.Add(this.label1);
69             this.Controls.Add(this.button1);
70             this.Name = "Form1";
71             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
72             this.Text = "Form1";
73             this.ResumeLayout(false);
74             this.PerformLayout();
75 
76         }
77 
78         #endregion
79 
80         private System.Windows.Forms.Button button1;
81         private System.Windows.Forms.Label label1;
82         private System.Windows.Forms.TextBox textBox1;
83     }
84 }
View Code

//Form2.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace WindowsFormsApplication2
 11 {
 12     public partial class Form2 : Form
 13     {
 14         //01通过传窗口引用进行数据传递 
 15         //private Form1 frm1;
 16         //public Form2(Form1 f)
 17         //{
 18         //    this.frm1 = f;
 19         //    InitializeComponent();
 20         //}
 21 
 22         //02通过传窗口引用进行数据传递
 23         //private TextBox tb;
 24         //public Form2(TextBox tb)
 25         //{
 26         //    this.tb = tb;
 27         //    InitializeComponent();
 28         //}
 29 
 30         //03通过接口类进行通讯
 31         //private Interface1 iChangeTitle;
 32         //public Form2(Interface1 iChangeTitle)
 33         //{
 34         //    InitializeComponent();
 35         //    this.iChangeTitle = iChangeTitle;
 36         
 37         //}
 38 
 39         //04通过委托实现窗体间通信
 40         //public delegate void TitleChangedHandler(String sTitle);//声明委托
 41         //public TitleChangedHandler TitleChanged;//定义委托
 42         //public Form2()
 43         //{
 44         //    InitializeComponent();
 45         //}
 46 
 47         //05通过自定义事件、事件参数进行窗体通信
 48         public class TitleChangedEventArgs : EventArgs
 49         { 
 50             private String sTitle;
 51             public String Title
 52             {
 53                 set { sTitle = value; }
 54                 get { return sTitle; }
 55             }
 56         }
 57 
 58         //声明事件委托
 59         public delegate void TitleChangedEventHandler(object sender, TitleChangedEventArgs e);
 60         //定义事件
 61         public event TitleChangedEventHandler TitleChanged;
 62 
 63         public Form2()
 64         {
 65             InitializeComponent();
 66         }
 67         
 68         private void button1_Click(object sender, EventArgs e)
 69         {
 70             //借助公共类在窗体间传递数据
 71             //this.Text = Class1.str;
 72 
 73             //01通过传窗口引用进行数据传递
 74             //frm1.Text = "通过传窗口引用进行数据传递";//修改父窗标题
 75 
 76             //TextBox tb = frm1.Controls[0] as TextBox;
 77             //this.Text = tb.Text;//利用父窗的textbox1控件内容修改子窗标题
 78 
 79             //02通过传窗口引用进行数据传递
 80             //this.Text = tb.Text;
 81 
 82             //03通过接口类进行通讯
 83             //iChangeTitle.ChangeTitle("通过接口类进行通讯");//改写父窗标题
 84          
 85             //04通过委托实现窗体间通信
 86             //if (TitleChanged != null)
 87             //{
 88             //    TitleChanged("04通过委托实现窗体间通信");
 89             //}
 90 
 91             //05通过自定义事件、事件参数进行窗体通信
 92             TitleChangedEventArgs eTitle = new TitleChangedEventArgs();
 93             eTitle.Title = "05通过自定义事件、事件参数进行窗体通信";
 94             OnTitleChanged(eTitle);
 95         }
 96 
 97         //05通过自定义事件、事件参数进行窗体通信
 98         protected virtual void OnTitleChanged(TitleChangedEventArgs e)
 99         {
100             if (TitleChanged != null)
101             {
102                 TitleChanged(this, e);
103             }
104         
105         }
106     }
107 }
View Code

//Form2.Designer.cs

 1 namespace WindowsFormsApplication2
 2 {
 3     partial class Form2
 4     {
 5         /// <summary>
 6         /// Required designer variable.
 7         /// </summary>
 8         private System.ComponentModel.IContainer components = null;
 9 
10         /// <summary>
11         /// Clean up any resources being used.
12         /// </summary>
13         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
14         protected override void Dispose(bool disposing)
15         {
16             if (disposing && (components != null))
17             {
18                 components.Dispose();
19             }
20             base.Dispose(disposing);
21         }
22 
23         #region Windows Form Designer generated code
24 
25         /// <summary>
26         /// Required method for Designer support - do not modify
27         /// the contents of this method with the code editor.
28         /// </summary>
29         private void InitializeComponent()
30         {
31             this.button1 = new System.Windows.Forms.Button();
32             this.SuspendLayout();
33             // 
34             // button1
35             // 
36             this.button1.Location = new System.Drawing.Point(78, 122);
37             this.button1.Name = "button1";
38             this.button1.Size = new System.Drawing.Size(150, 70);
39             this.button1.TabIndex = 0;
40             this.button1.Text = "取得母窗体传过来的文本作为窗体标题";
41             this.button1.UseVisualStyleBackColor = true;
42             this.button1.Click += new System.EventHandler(this.button1_Click);
43             // 
44             // Form2
45             // 
46             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
47             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
48             this.ClientSize = new System.Drawing.Size(292, 266);
49             this.Controls.Add(this.button1);
50             this.MaximizeBox = false;
51             this.Name = "Form2";
52             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
53             this.Text = "Form2";
54             this.ResumeLayout(false);
55 
56         }
57 
58         #endregion
59 
60         private System.Windows.Forms.Button button1;
61     }
62 }
View Code

//Class1.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace WindowsFormsApplication2
 7 {
 8     //00借助公共类在窗体间传递数据
 9     //class Class1
10     //{
11     //    public static string str;
12     //}
13 }
View Code

//Interface1.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace WindowsFormsApplication2
 7 {
 8     public interface Interface1
 9     {
10         void ChangeTitle(String sTitle);
11     }
12 }
View Code

//Program.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Windows.Forms;
 5 
 6 namespace WindowsFormsApplication2
 7 {
 8     static class Program
 9     {
10         /// <summary>
11         /// 应用程序的主入口点。
12         /// </summary>
13         [STAThread]
14         static void Main()
15         {
16             Application.EnableVisualStyles();
17             Application.SetCompatibleTextRenderingDefault(false);
18             Application.Run(new Form1());
19         }
20     }
21 }
View Code

 

posted @ 2013-10-13 21:24  客栈老人  阅读(1731)  评论(0编辑  收藏  举报