c#中在一个窗体中触发另一个窗体的事件

c#中在⼀个窗体中触发另⼀个窗体的事件创建两个窗体,分别为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)
{undefined
if (MyEvent != null)
MyEvent();//引发事件
this.Close();
}
在form1的代码中添加函数
void b_MyEvent()
{undefined
按钮\r\n";
}
修改form1的构造函数
public Form1()
旋转座椅{undefined
InitializeComponent();
b = new Form2();//实例化b窗体
b.MyEvent += new Form2.MyDelegate(b_MyEvent);//监听b窗体事件
}
为form1中的button1添加消息响应函数
private void button1_Click(object sender, EventArgs e)
{undefined
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;led调光电源
揉棉机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()
{
}
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();
}
}
}

本文发布于:2024-09-22 18:15:06,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/186268.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:窗体   事件   添加   函数   按钮
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议