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());
}
}
}
C#实现窗体间的通信的更多相关文章
- C# 委托和事件 实现窗体间的通信
例子 : 点击form1上的按钮打开form2窗口,在form2窗体中的文本框中输入一个值后,在点击form2窗体中按钮,在form2中的文本框中输入的值也会在form1中的文本框中出现. form1 ...
- qt 窗体间通信
利用qt的信号和槽,可以完成窗体间的通信,下面列出父子窗口利用信号和槽的相关代码. parent窗口: //parent.h #ifndef PARENT_H #define PARENT_H #in ...
- c# 进程间的通信实现之一简单字符串收发
使用Windows API实现两个进程间(含窗体)的通信在Windows下的两个进程之间通信通常有多种实现方式,在.NET中,有如命名管道.消息队列.共享内存等实现方式,这篇文章要讲的是使用Wi ...
- C#不同窗体间通信,数据传递
在一个项目中,很多时候都需要在窗体间进行数据传递和通信,最觉见的是父子窗体之间的数据传递,比如登录ID,各个窗体都需要知道.有很多文章都写了这方面的问题,提出很多优秀的方法,鄙人不才,搜了一些资料之后 ...
- winform利用委托delegate进行窗体间通信
前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...
- winform利用委托delegate进行窗体间通信,相同标题已经存在??
前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...
- C# 调用Windows API实现两个进程间的通信
使用Windows API实现两个进程间(含窗体)的通信http://blog.csdn.net/huangxinfeng/article/details/5513608 从C#下使用WM_COPYD ...
- 总结几种C#窗体间通讯的处理方法
摘要:本文介绍了C#窗体间通讯的几种处理方法,即传值.继承.事件回调,希望对大家有用. http://www.cnblogs.com/jara/p/3439603.html 应用程序开发中,经常需要多 ...
- QT窗体间传值总结之Signal&Slot
在写程序时,难免会碰到多窗体之间进行传值的问题.依照自己的理解,我把多窗体传值的可以使用的方法归纳如下: 1.使用QT中的Signal&Slot机制进行传值: 2.使用全局变量: 3.使用pu ...
随机推荐
- uva 10034 Problem A: Freckles
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...
- VS2013中使用GDI+绘图
VC范例,400多个例子源代码下载 http://download.csdn.net/detail/bigtree_mfc/7727977 VS2013中使用GDI+绘图和VC6.0不同,在VC6.0 ...
- WIN7下关闭驱动数字签名检查的方法
内容是转的,最后一步貌似没什么用处,水印是去不掉的,不过也无所谓,关键是驱动能用了,要不完全瞎了 实测win7 32位旗舰版可用 ================================= ...
- LinkedList和ArrayList的区别
LinkedeList和ArrayList都实现了List接口,但是它们的工作原理却不一样.它们之间最主要的区别在于ArrayList是可改变大小的数组,而LinkedList是双向链接串列(doub ...
- WPF自定义控件与样式(15)-终结篇
原文:WPF自定义控件与样式(15)-终结篇 系列文章目录 WPF自定义控件与样式(1)-矢量字体图标(iconfont) WPF自定义控件与样式(2)-自定义按钮FButton WPF自定义控件与 ...
- mysql之事务
事务处理 begin 开始一个事物 commit 事务确认 rollback 事务回滚 end 事务结束 innodb下可以实现事务 开始执行事务时如果 ...
- B450配置
电脑型号 联想 B450 笔记本电脑 (扫描时间:2015?5?7?操作系统 Windows 7 旗舰版 32位 SP1 ( DirectX 11 ) 处理器 英特尔 酷睿2 双核 T9800 @ ...
- google开源的C++性能分析工具 - gperftools
gperftools是Google提供的一套工具,其中的一个功能是CPU profiler,用于分析程序性能,找到程序的性能瓶颈. 安装 gperftools:http://code.google.c ...
- delphi7调用java写的webservice,在调用的时候弹出“wssecurityhandler:request does not contain required security header”
delphi7调用java编写的webservice问题我用delphi7调用java写的webservice,在调用的时候弹出“wssecurityhandler:request does not ...