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 ...
随机推荐
- TableView_图片异步加载 KVO
TableView 异步下载图片 ImageDownloader.h #pragma mark - 声明block //1,声明block typedef void(^Result) (UIImage ...
- Web前端开发
由于互联网的各种兴起,网页开发似乎也火了,催生了github上各种js的轮子,各种重复,各种框架和库,什么Jquery,bootstrap等等.面对这么多框架和库我们在工程上该如何取舍(trade-o ...
- git设置过滤忽略的文件或文件夹
我们一般向代码仓库提交项目的时候,一般需要忽略编译生成的中间文件以及文件夹的提交,因为它们是无用的,而且也会占用仓库的空间.一般只用提交.pro,.sln,makefile,程序源文件等编译必须用到的 ...
- 【转】listView中,checkBox的显示和隐藏
原文网址:http://www.cnblogs.com/vicma/p/3460500.html 在listView中,每个item都有一个ChexBox,当显示的时候在listView外面设置一个按 ...
- 百度搜索URL参数 搜索关键字
http://www.baidu.com/s?wd=关键字 wd(Keyword):查询的关键词: http://www.baidu.com/s?wd=关键字&cl=3 cl(Class):搜 ...
- HDU4605---Magic Ball Game(主席树 好题)
题意:一颗二叉树,任意节点要么有两个孩子要么没孩子. 然后有一个球,从结点1开始往子孙结点走. 每碰到一个结点,有三种情况 如果此球重量等于该结点重量,球就停下了 如果此球重量小于该结点重量,则分别往 ...
- 让背景图片跟随div大小变化代码
- 使用python程序监控云服务器的带宽
将一些不重要的服务迁移到云服务上,有些下载什么的,为了防止带宽超了,python做了一个监控程序.用python3写的,由于和python2有些区别,特记录下来备查. 代码如下: #this is f ...
- double精度的坑与BigDecimal
近期经常接触支付相关的功能,在开发及测试过程中,开始金额都使用的是double类型,而近期新进的需求存在支付时打折的情况,也就是会出现如 1.23元的情况,那么这时候问题来了,如果是直接使用1.23进 ...
- 苹果推送APNS总结 (
开发状态服务器地址 gateway.sandbox.push.apple.com 2195产品状态服务器地址 gateway.push.apple.com 2195 Developme ...