以下将窗体间的几种通信实现方式做一下罗列:首先新建一个窗体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());
}
}
}

C#实现窗体间的通信的更多相关文章

  1. C# 委托和事件 实现窗体间的通信

    例子 : 点击form1上的按钮打开form2窗口,在form2窗体中的文本框中输入一个值后,在点击form2窗体中按钮,在form2中的文本框中输入的值也会在form1中的文本框中出现. form1 ...

  2. qt 窗体间通信

    利用qt的信号和槽,可以完成窗体间的通信,下面列出父子窗口利用信号和槽的相关代码. parent窗口: //parent.h #ifndef PARENT_H #define PARENT_H #in ...

  3. c# 进程间的通信实现之一简单字符串收发

       使用Windows API实现两个进程间(含窗体)的通信在Windows下的两个进程之间通信通常有多种实现方式,在.NET中,有如命名管道.消息队列.共享内存等实现方式,这篇文章要讲的是使用Wi ...

  4. C#不同窗体间通信,数据传递

    在一个项目中,很多时候都需要在窗体间进行数据传递和通信,最觉见的是父子窗体之间的数据传递,比如登录ID,各个窗体都需要知道.有很多文章都写了这方面的问题,提出很多优秀的方法,鄙人不才,搜了一些资料之后 ...

  5. winform利用委托delegate进行窗体间通信

    前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...

  6. winform利用委托delegate进行窗体间通信,相同标题已经存在??

    前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...

  7. C# 调用Windows API实现两个进程间的通信

    使用Windows API实现两个进程间(含窗体)的通信http://blog.csdn.net/huangxinfeng/article/details/5513608 从C#下使用WM_COPYD ...

  8. 总结几种C#窗体间通讯的处理方法

    摘要:本文介绍了C#窗体间通讯的几种处理方法,即传值.继承.事件回调,希望对大家有用. http://www.cnblogs.com/jara/p/3439603.html 应用程序开发中,经常需要多 ...

  9. QT窗体间传值总结之Signal&Slot

    在写程序时,难免会碰到多窗体之间进行传值的问题.依照自己的理解,我把多窗体传值的可以使用的方法归纳如下: 1.使用QT中的Signal&Slot机制进行传值: 2.使用全局变量: 3.使用pu ...

随机推荐

  1. Unity中的各种寻找GameObject方法

    1.GameObject.Find():寻找Hierarchy面板中的activie 不为false的游戏对象: 路径如官方事例写法: public class ExampleClass : Mono ...

  2. Oracle基本分组查询group by的使用

    (1)查询各个工作岗位的总人数 (2)查询各个工作岗位的总工资 (3)查询各个工作岗位的平均工资

  3. Entity Framework with MySQL 学习笔记一(安装)

    声明 :  数据库是Mysql,本人的程度只到会写sql语句(不会储蓄过程), c# 会基本的ADO.NET数据库访问,LINQ基础. 这篇只做个人学习|温习作用. 新手可以参考,也请高手指正错误, ...

  4. iconv any encoding to UTF-8

    http://stackoverflow.com/questions/9824902/iconv-any-encoding-to-utf-8

  5. 如何判断一个C++对象是否在堆上(通过GetProcessHeaps取得所有堆,然后与对象地址比较即可),附许多精彩评论

    在帖子如何判断一个C++对象是否在堆栈上 中, 又有人提出如何判断一个C++对象是否在堆上. 其实我们可以参照那个帖子的方法类似实现,我们知道堆就是Heap,在windows上我们可以通过GetPro ...

  6. 2个Web上传组件

    http://www.uploadify.com/download/ http://gmupload.tanjun.com.cn/

  7. Linux/Ubuntu下 静态编译Qt程序

    一般情况下,我们用Qt编译出来的程序是要依赖于系统Qt库的,也就是这个程序移到别的没有安装Qt库的系统上是不能使用的.会提示缺少……库文件之类的错误.这就是动态编译的结果. 但是如果我们想编译一个程序 ...

  8. 模糊集合和隶属度函数--AForge.NET框架的使用(一)

    原文:模糊集合和隶属度函数--AForge.NET框架的使用(一) 什么是AForge.NET? AForge.NET是一个为开发人员和研究人员开发的框架,它可以用于计算机视觉,遗传算法,图像处理,神 ...

  9. xcode教程,MAC常用命令

    Vmware中为Mac Os安装vmtools 开机后,在Finder中就可以找到Darwin.iso,进行安装. 附件:vmtools for macos下载地址 https://softwareu ...

  10. 通过JS触发TextBox的ontextchanged事件,并获取TextBox所在GridView的那一行

    protected void txtInsNum_TextChanged(object sender, EventArgs e) { TextBox t = (TextBox)sender; Grid ...