以下将窗体间的几种通信实现方式做一下罗列:首先新建一个窗体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. iOS开发之UIWebView自动滑动到顶部-备

    但可以通过subview来操作. 通常用UIWebView加载网页,有时候需要点击一个按钮,或者页面的某个部位,使页面自动滚动到顶部,但是UIWebView不像UIScrollView那么方便.   ...

  2. Windows下python安装matplotlib

    此文为转载,原文地址为:http://blog.csdn.net/u010585135/article/details/42127273 一.下载matplotlib安装包:网址http://matp ...

  3. d3可视化实战01:理解SVG元素特性

    一. SVG简介 ————————————————————————————————————————————————————————————————— SVG是一种和图像分辨率无关的矢量图形格式,它使用 ...

  4. ural 1126 Magnetic Storms

    http://acm.timus.ru/problem.aspx?space=1&num=1126 #include <cstdio> #include <cstring&g ...

  5. Java JDBC中,MySQL字段类型到JAVA类型的转换

    1. 概述 在使用Java JDBC时,你是否有过这样的疑问:MySQL里的数据类型到底该选择哪种Java类型与之对应?本篇将为你揭开这个答案. 2. 类型映射  java.sql.Types定义了常 ...

  6. js 数组引用 发现的问题

    最近做项目时,要对返回的数据[保存在json数组中]做一次修改,但原数据要保留一次做备用.首先想到,原数据不动,用一个临时的变量来修改,大致模型就是这样: // 原始: a=[1,2,3,4,5,.. ...

  7. Android视频录制

    public class MainActivity extends Activity { private MediaRecorder videoRecorder=null; private Butto ...

  8. Tomcat 改变localhost主页,映射到应用地址

    <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWA ...

  9. appium windows 命令行中运行以及targetSdkVersionFromManifest failed的解决

    启动appium服务,可以通过appium.exe可执行文件启动,也可以通过命令行启动.appium.exe启动需要通过安装可执行文件,命令行启动需要通过npm安装appium.可执行文件启动方式如下 ...

  10. HTML获取用户输入的几种玩法

    input标签 input是一个自闭和标签,可以获得用户的输入 form标签 form标签是用来进行表单提交用的,它把用户的输入内容提交到服务器. 一个注册页面的例子 <!DOCTYPE htm ...