如何正确监听其他表单的表单事件 [英] How to properly listen form events with another form

查看:59
本文介绍了如何正确监听其他表单的表单事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在收听事件中陷入另一种形式.

Stuck in listening event to another form.

当我尝试关闭Form2时,Form1上没有任何反应.我想在Form2关闭时在Form1中做一些事情.

When I try to close my Form2, nothing happens on Form1. I want to do something in Form1 when Form2 closes.

这是我的Form1代码

Here is my code for Form1

public partial class Form1: Form
        {
             public Form1()
            {
                InitializeComponent();

                Form2 frm2= new Form2();
                frm2.FormClosing += new FormClosingEventHandler(frm2_FormClosing);
            }

            void frm2_FormClosing(object sender, FormClosingEventArgs e)
            {
                throw new NotImplementedException();
            }

推荐答案

您需要显示要实现的对象,该对象是FormClosing事件.由于您要创建的新对象位于构造函数中,因此我假设frm2不是您要显示的Form,这意味着您没有处理该事件.

You will need to show the object which you are implementing it's FormClosing event. Since the new object you are creating is in your constructor I assume that frm2 isn't the Form which you are showing, which means you are not handling the event.

public Form1()
{
    InitializeComponent();

    Form2 frm2 = new Form2();
    frm2.FormClosing += frm2_FormClosing;
    frm2.Show();    
}

void frm2_FormClosing(object sender, FormClosingEventArgs e)
{
    MessageBox.Show("Form2 is closing");
}

这篇关于如何正确监听其他表单的表单事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆