c#中在一个窗体中触发另一个窗体的事件
2017-08-07 10:05
696 查看
创建两个窗体,分别为form1,form2,在form1中添加控件textBox1和button1,创建一个form2的对象Form2 b = null;
在form2中添加button1,定义委托和事件
//定义委托
public delegate void MyDelegate();
//定义事件
public event MyDelegate MyEvent;
给form2中的button1添加消息相应函数并做修改
private void button1_Click(object sender, EventArgs e)
{
if (MyEvent != null)
MyEvent();//引发事件
this.Close();
}
在form1的代码中添加函数
void b_MyEvent()
{
this.textBox1.Text += "已单击b窗体按钮\r\n";
}
修改form1的构造函数
public Form1()
{
InitializeComponent();
b = new Form2();//实例化b窗体
b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
}
为form1中的button1添加消息响应函数
private void button1_Click(object sender, EventArgs e)
{
b.ShowDialog();
}
这样当单击form1中的按钮时会弹出form2,当单击form2中的按钮时,form1中的textbox1会显示“已单击b窗体按钮”。
具体代码如下(vs 2005实现):
form1代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace form1
{
public partial class Form1 : Form
{
Form2 b = null;
public Form1()
{
InitializeComponent();
b = new Form2();//实例化b窗体
b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
}
void b_MyEvent()
{
this.textBox1.Text += "已单击b窗体按钮\r\n";
}
private void button1_Click(object sender, EventArgs e)
{
b.ShowDialog();
}
}
}
form2代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace form1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//定义委托
public delegate void MyDelegate();
//定义事件
public event MyDelegate MyEvent;
private void button1_Click(object sender, EventArgs e)
{
if (MyEvent != null)
MyEvent();//引发事件
this.Close();
}
}
}
文章来源:http://blog.163.com/liujiyun123@126/blog/static/4459677620108295850128/
在form2中添加button1,定义委托和事件
//定义委托
public delegate void MyDelegate();
//定义事件
public event MyDelegate MyEvent;
给form2中的button1添加消息相应函数并做修改
private void button1_Click(object sender, EventArgs e)
{
if (MyEvent != null)
MyEvent();//引发事件
this.Close();
}
在form1的代码中添加函数
void b_MyEvent()
{
this.textBox1.Text += "已单击b窗体按钮\r\n";
}
修改form1的构造函数
public Form1()
{
InitializeComponent();
b = new Form2();//实例化b窗体
b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
}
为form1中的button1添加消息响应函数
private void button1_Click(object sender, EventArgs e)
{
b.ShowDialog();
}
这样当单击form1中的按钮时会弹出form2,当单击form2中的按钮时,form1中的textbox1会显示“已单击b窗体按钮”。
具体代码如下(vs 2005实现):
form1代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace form1
{
public partial class Form1 : Form
{
Form2 b = null;
public Form1()
{
InitializeComponent();
b = new Form2();//实例化b窗体
b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
}
void b_MyEvent()
{
this.textBox1.Text += "已单击b窗体按钮\r\n";
}
private void button1_Click(object sender, EventArgs e)
{
b.ShowDialog();
}
}
}
form2代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace form1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
//定义委托
public delegate void MyDelegate();
//定义事件
public event MyDelegate MyEvent;
private void button1_Click(object sender, EventArgs e)
{
if (MyEvent != null)
MyEvent();//引发事件
this.Close();
}
}
}
文章来源:http://blog.163.com/liujiyun123@126/blog/static/4459677620108295850128/
相关文章推荐
- [转]c#中在一个窗体中触发另一个窗体的事件
- c#中在一个窗体中触发另一个窗体的事件 (转载)
- C# winform一个窗口按钮触发另一个窗口的load事件
- Winform wpf在一个窗体的事件里,修改另一个窗体控件的值
- C# winform中一个类中如何调用另一个窗体的控件或方法
- WinForm中一个窗体调用另一个窗体的控件和事件的方法(附带源码)
- 如何通过按钮触发事件由一个jsp页面转到另一个jsp页面
- JavaScript点击事件/一个按钮触发另一个按钮
- WinForm将一个窗体的值传到另一个窗体的listbox控件,C#
- WinForm中一个窗体调用另一个窗体的控件和事件的方法(附带源码)
- JavaScript点击事件-一个按钮触发另一个按钮
- 初学C#,请问怎么在一个窗体中调用另一个窗体?
- C# 使用Panel控件实现在一个窗体中嵌套另一个窗体
- C#程序如何把窗体文件从从一个项目中复制到另一个项目
- c#中一个窗体调用另一个窗体的控件及方法(转贴)
- C#如何在一个按钮事件中调用另一个按钮的事件代码
- vb.net 在一个窗体中,可不可以RaiseEvent另一个窗体(或者Class)中的事件?
- 利用 perform 来触发另一个窗体的事件
- C#在一个解决方案下怎么在一个窗体中调用另一个窗体中的方法
- C# WinForm窗体的TopMost属性设置为true时,多次点击窗体弹出事件,只会弹出一个窗体