C#基础练习(使用委托窗体传值)
2016-01-15 21:50
591 查看
主界面:
Form1中的代码:
namespace _06委托练习_窗体传值
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2(txt1.Text,DoSth);//传过去一个字符串和DoSth方法
f2.Show();
}
//把字符串变量的值赋值给文本框
public void DoSth(string str)
{
this.txt1.Text = str;
}
}
}
Form2中的代码:
namespace _06委托练习_窗体传值
{
public delegate void MyDel(string str);//定义一个委托
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private MyDel _mdl;//实例化一个委托变量
public Form2(string str,MyDel mdl):this()
{
this.txt2.Text = str;
this._mdl = mdl;
}
private void btn2_Click(object sender, EventArgs e)
{
if (this._mdl!=null)
{
this._mdl(txt2.Text);
this.Close();
}
}
}
}
Form1中的代码:
namespace _06委托练习_窗体传值
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
Form2 f2=new Form2(txt1.Text,DoSth);//传过去一个字符串和DoSth方法
f2.Show();
}
//把字符串变量的值赋值给文本框
public void DoSth(string str)
{
this.txt1.Text = str;
}
}
}
Form2中的代码:
namespace _06委托练习_窗体传值
{
public delegate void MyDel(string str);//定义一个委托
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private MyDel _mdl;//实例化一个委托变量
public Form2(string str,MyDel mdl):this()
{
this.txt2.Text = str;
this._mdl = mdl;
}
private void btn2_Click(object sender, EventArgs e)
{
if (this._mdl!=null)
{
this._mdl(txt2.Text);
this.Close();
}
}
}
}
相关文章推荐
- C#基础练习(使用委托窗体传值)
- c# 反射
- C#关于Clone()方法的介绍
- .NET中提升UAC权限的方法总结
- c# 深拷贝 浅拷贝
- Silverlight 5(C#)初探
- C#Socket 文件传输,支持断点续传
- c#下利用winAPI来实现扩展屏的分辨率设置、方向切换等
- NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(2)
- NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(2)
- C# 中dataTable中的数据批量增加至数据表中
- C#线程同步
- C# 上传图片前判断上传图片的宽和高
- 从0自学C#01--自绘窗体边框
- C#基础语法实例荟萃
- c#实现OutLook定时会议室申请
- C#实现UDP协议
- [UDP] C#UDP入门|Socket通信|UdpClient通信
- c# webbrowser获取cookies
- 关于C#中”扩展方法必须在非泛型静态类中定义“问题的解决