C#实现窗体间的通信

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

//Form1.cs

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
//03通过接口类进行通讯
//public partial class Form1 : Form, Interface1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//00借助公共类在窗体间传递数据
//Class1.str = textBox1.Text;
//Form2 frm2 = new Form2();
//frm2.Show(); //01通过传窗口引用进行数据传递
//Form2 frm2 = new Form2(this);
//frm2.Show(); //02通过传窗口引用进行数据传递
//Form2 frm2 = new Form2(this.textBox1);
//frm2.Show(); //03通过接口类进行通讯
//Form2 frm2 = new Form2(this);
//frm2.Show(); //04通过委托实现窗体间通信
//Form2 frm2 = new Form2();
//frm2.TitleChanged = new Form2.TitleChangedHandler(TitleChanged);
//frm2.Show(); //05通过自定义事件、事件参数进行窗体通信
Form2 frm2 = new Form2();
frm2.TitleChanged +=new Form2.TitleChangedEventHandler(FrmTitleChanged);
frm2.Show();
} //03通过接口类进行通讯
//public void ChangeTitle(String sTitle)
//{
// this.Text = sTitle;
//} //04通过委托实现窗体间通信
//protected void TitleChanged(String sTitle)
//{
// this.Text = sTitle;
//} //05通过自定义事件、事件参数进行窗体通信
public void FrmTitleChanged(object sender, Form2.TitleChangedEventArgs e)
{
this.Text = e.Title;
} }
}

//Form1.Designer.cs

 namespace WindowsFormsApplication2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "打开子窗口";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(, );
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "设置子窗口的标题为:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(, );
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(, );
this.textBox1.TabIndex = ;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
}
}

//Form2.cs

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
//01通过传窗口引用进行数据传递
//private Form1 frm1;
//public Form2(Form1 f)
//{
// this.frm1 = f;
// InitializeComponent();
//} //02通过传窗口引用进行数据传递
//private TextBox tb;
//public Form2(TextBox tb)
//{
// this.tb = tb;
// InitializeComponent();
//} //03通过接口类进行通讯
//private Interface1 iChangeTitle;
//public Form2(Interface1 iChangeTitle)
//{
// InitializeComponent();
// this.iChangeTitle = iChangeTitle; //} //04通过委托实现窗体间通信
//public delegate void TitleChangedHandler(String sTitle);//声明委托
//public TitleChangedHandler TitleChanged;//定义委托
//public Form2()
//{
// InitializeComponent();
//} //05通过自定义事件、事件参数进行窗体通信
public class TitleChangedEventArgs : EventArgs
{
private String sTitle;
public String Title
{
set { sTitle = value; }
get { return sTitle; }
}
} //声明事件委托
public delegate void TitleChangedEventHandler(object sender, TitleChangedEventArgs e);
//定义事件
public event TitleChangedEventHandler TitleChanged; public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//借助公共类在窗体间传递数据
//this.Text = Class1.str; //01通过传窗口引用进行数据传递
//frm1.Text = "通过传窗口引用进行数据传递";//修改父窗标题 //TextBox tb = frm1.Controls[0] as TextBox;
//this.Text = tb.Text;//利用父窗的textbox1控件内容修改子窗标题 //02通过传窗口引用进行数据传递
//this.Text = tb.Text; //03通过接口类进行通讯
//iChangeTitle.ChangeTitle("通过接口类进行通讯");//改写父窗标题 //04通过委托实现窗体间通信
//if (TitleChanged != null)
//{
// TitleChanged("04通过委托实现窗体间通信");
//} //05通过自定义事件、事件参数进行窗体通信
TitleChangedEventArgs eTitle = new TitleChangedEventArgs();
eTitle.Title = "05通过自定义事件、事件参数进行窗体通信";
OnTitleChanged(eTitle);
} //05通过自定义事件、事件参数进行窗体通信
protected virtual void OnTitleChanged(TitleChangedEventArgs e)
{
if (TitleChanged != null)
{
TitleChanged(this, e);
} }
}
}

//Form2.Designer.cs

 namespace WindowsFormsApplication2
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
this.button1.Text = "取得母窗体传过来的文本作为窗体标题";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form2";
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1;
}
}

//Class1.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WindowsFormsApplication2
{
//00借助公共类在窗体间传递数据
//class Class1
//{
// public static string str;
//}
}

//Interface1.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace WindowsFormsApplication2
{
public interface Interface1
{
void ChangeTitle(String sTitle);
}
}

//Program.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
上一篇:2015年Java开发岗位面试题归类


下一篇:版本管理工具 —— SVN