A、网上有很多方法,大家可搜一下,都可用。
B、这里只是说明下是只利用委托,学习基本的委托运用。
方法如下:
1、C#建立一个默认工程,默认窗体Form1
2、加入一个新窗体,默认Form2
3、Form1窗体上放一个Label,一个Button,属性分别为:
this.label1.Text = ""; // 清空label的值
this.button1.Text="otherForm"; //设置button显示为otherForm
view plaincopy to clipboardprint?
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
4、Form2窗体上放一个textbox1,一个Button,属性分别为:
this.textbox1.Text = ""; //清空textbox的值
this.button1.Text = "input"; //设置button显示为input(输入)
view plaincopy to clipboardprint?
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
5、上面的步骤就建立了两个基本的窗体,接下来回到Form1的代码设计视图
拷贝下面代码
view plaincopy to clipboardprint?
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
returnValueEvent += new returnValue(Form1_returnValueEvent);
frm2.Show();
}
void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}
// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}
}
}
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.label1.Text = "";
this.button1.Text = "otherForm";
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
returnValueEvent += new returnValue(Form1_returnValueEvent);
frm2.Show();
}
void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}
// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}
}
}
6、在Form2的代码设计视图中,拷贝下面代码
view plaincopy to clipboardprint?
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);
}
}
}
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.textBox1.Text = "";
this.button1.Text = "input";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);
}
}
}
7、运行后,就可以在两个窗体间传递参数了
注:重点是第六步中的,对委托的定义和调用
returnValueEvent += new returnValue(Form1_returnValueEvent);
void Form1_returnValueEvent(string str)
{
// throw new NotImplementedException();
this.label1.Text = str;
}
// 定义委托
public delegate void returnValue(string str);
// 委托的事件
public static event returnValue returnValueEvent;
// 委托的事件的函数
public void doReturnValue(string str)
{
returnValueEvent(str);
}
在Form2中的调用,在按钮的click事件中
Form1 frm1 = new Form1();
frm1.doReturnValue(this.textBox1.Text);
相关推荐
C#利用委托实现窗体传值
本人做的一个非常简单 C#委托的例子,很基础很简单的一个小例子,最最适合委托初学者参看。再结合资料,相信学通C#委托...委托很好的解决了窗体之间的数据传递和方法调用,非常实用,通过这个例子相信能让你恍然大悟。
两个DataGridView通过委托实时传值,安全高效 C# 两个窗体之间实时传递数据的实例。利用委托和事件的方法,从子窗体传送数据到主窗体。比较安全。
###方法2:通过委托,在子窗体显示之前,为委托赋值,关注主窗体的数据变化,当有当有多个窗体需要接收信息,只需要为委托继续赋值(+=)即可,实现了数据传递的解耦性; ###方法3:子窗体弹出来之前,注册事件,...
C# 使用委托方式Usercontrol与主窗体进行消息传递
C# 两个窗体之间实时传递数据的实例。利用委托和事件的方法,从子窗体传送数据到主窗体。比较安全。
C#利用委托来进行窗体间传递复杂数据的实例,可以参考一下
C#语言Winform下采用委托事件方法实现窗体间信息传递
很简单的代码, 以委托方式进行窗体间消息传递,可以同时发送给多个窗体(多播委托)
c# 委托,子窗体往父窗体传值、调用方法,DEMO,完整代码,visual studio2010
本文实例讲述了C#使用委托(delegate)实现在两个form之间传递数据的方法。分享给大家供大家参考。具体分析如下: 关于Delegate【代理、委托】是C#中一个非常重要的概念,向前可以推演到C++的指针,向后可以延续到匿名...
C# 使用Usercontrol与主窗体进行消息传递,利用Usercontrol里面的控件控制主窗体上的控件;利用主窗体的控件改变Usercontrol里面控件的属性
使用委托技术实现winform编程多窗体直接的值传递使用委托技术实现winform编程多窗体直接的值传递使用委托技术实现winform编程多窗体直接的值传递使用委托技术实现winform编程多窗体直接的值传递使用委托技术实现...
本文件用通俗易懂的代码展示了多窗体之间的数据传递,主要使用了委托
使用委托,事件和BeginInvoke从另外一个cs文件向form.cs文件传递数据,并把数据赋值给窗体控件
该资源是C#不同窗体间的数据传递发布订阅模式(观察者模式)源码范例,资源很好,思路清晰,值得大家借鉴
C# WinForm之间调用、窗口之间相互传递参数的例子
WinForm窗体之间参数传递(委托加事件观察者模式实现)
winform 窗体间通过属性和委托传递值。无需更改,直接调试即可。
c#源码Demo利用委托方式实现父窗体同时传递信息给多个子窗体,不同子窗体发送消息给父窗体相关技术。