C#使用委托从子窗体向主窗体发送消息
2017-05-11 07:53
465 查看
C#经常会使用到窗体的交互,需要共享数据
/*
* 【1】申明委托(定义一个函数原型:返回值,参数类型和个数)
* 【2】根据委托,定义一个目标方法(目标方法可以有多个),注意目标方法和委托的返回值以及参数类型个数必须保持一致
* 【3】创建委托对象,关联目标方法
* 【4】通过委托来调用方法,而不是直接使用方法
*/
主窗体代码:
public delegate void GetExposeTimeDelegate(string count);//【1】
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
ChildForm objectChildForm = new ChildForm();
objectChildForm.objectGetExposeTimeDelegate += GetExposeTime;//【3】
objectChildForm.Show();
}
public void GetExposeTime(string count)//【2】
{
lblCount.Text = count;
}
}
子窗体代码:
public ChildForm()
{
InitializeComponent();
}
public string strCount;
public GetExposeTimeDelegate objectGetExposeTimeDelegate;//【3】委托变量,作为传递信息的桥梁
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
strCount = Convert.ToString(ExposeTimeCount.Value);
objectGetExposeTimeDelegate(strCount);//【4】
}
/*
* 【1】申明委托(定义一个函数原型:返回值,参数类型和个数)
* 【2】根据委托,定义一个目标方法(目标方法可以有多个),注意目标方法和委托的返回值以及参数类型个数必须保持一致
* 【3】创建委托对象,关联目标方法
* 【4】通过委托来调用方法,而不是直接使用方法
*/
主窗体代码:
public delegate void GetExposeTimeDelegate(string count);//【1】
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
ChildForm objectChildForm = new ChildForm();
objectChildForm.objectGetExposeTimeDelegate += GetExposeTime;//【3】
objectChildForm.Show();
}
public void GetExposeTime(string count)//【2】
{
lblCount.Text = count;
}
}
子窗体代码:
public ChildForm()
{
InitializeComponent();
}
public string strCount;
public GetExposeTimeDelegate objectGetExposeTimeDelegate;//【3】委托变量,作为传递信息的桥梁
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
strCount = Convert.ToString(ExposeTimeCount.Value);
objectGetExposeTimeDelegate(strCount);//【4】
}
相关文章推荐
- C#使用委托实现主窗体向子窗体发送消息
- c# 使用委托例子 发送给父窗口消息
- 使用C#在应用程序间发送消息(转)
- 使用C#在应用程序间发送消息
- C#中在窗体间使用消息来处理相关联的事件
- C#可调用API接口来获取窗口句柄,发送消息控制其余程序窗体大小
- C# 使用重载消息处理函数的方式,实现没有标题栏的窗体的拖动。
- 使用C#在应用程序间发送消息
- C#中在窗体间使用消息来处理相关联的事件
- C# 使用WM_NCLBUTTONDOWN消息实现任意位置移动窗体
- C# 窗体间传值(使用委托与自定义事件)
- 使用C#在应用程序间发送消息
- 使用C#在应用程序间发送消息
- 使用C#在应用程序间发送消息 kongxx[原作]
- C#中使用 SendMessage 向非顶端窗体发送组合键
- 使用C#在应用程序间发送消息
- 使用C#在应用程序间发送消息
- 使用C#在应用程序间发送消息
- C#委托-在窗体中使用委托和事件
- 使用C# 向记事本窗口发送消息