表单结束事件保持循环MessageBox [已解决] [英] Form Closing Event Keeps Looping MessageBox[Solved]

查看:86
本文介绍了表单结束事件保持循环MessageBox [已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在解决问题并找到问题时遇到了一些问题。我现在花了4个多小时,我似乎没有取得任何进展。



这是我用来重现我的问题的一些代码一个新项目。

Hello, I'm having some trouble troubleshooting a problem and finding a fix for it. I've spent a little over 4 hours now and I don't seem to be making any progress.

Here is some code that I used to recreate my problem in a new project.

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {

        frmPopup frm;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (frm != null && !frm.IsDisposed) frm.Dispose();

            frm = new frmPopup();
            frm.FormClosing += frm_FormClosing;
            frm.Show();
        }
       
        void frm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("Test 1");
      
            MessageBox.Show("Test 2");
           
        }             
              
    }
}





在上面的例子中代码,'frmPopup'只是一个空白表单,当它的停用事件被触发时将调用this.Close()。这样,用户可以在表单外部单击以继续并关闭它,而无需每次都单击X按钮。在Form1上,捕获了frmPopup的结束事件,因此我可以在弹出窗口关闭时执行代码。



但是,在结束时由于某种原因event,显示Test 1的第一个MessageBox将在关闭后反复执行,第二个MessageBox将永远不会执行。我添加了以下代码来测试方法并确切了解发生了什么。





In the above example code, 'frmPopup' is just a blank form that will call "this.Close()" when its deactivated event is fired. This way a user can click outside of the form to go ahead and close it, without having to click the "X" button every time. On "Form1", the closing event of "frmPopup" is captured so I can execute code when the popup form is closing.

However, for some reason during the closing event, the first MessageBox that displays "Test 1", will execute over and over again after closing it, and the second MessageBox will never execute. I added the following code to test the method and see exactly what was happening.

int countOne = 0;
       int countTwo = 0;
       int countThree = 0;
       void frm_FormClosing(object sender, FormClosingEventArgs e)
       {
           countOne++;
           MessageBox.Show(string.Format("Test 1{0}Count One: {1}{0}Count Two: {2}{0}Count Three: {3}", System.Environment.NewLine, countOne, countTwo, countThree));
           countTwo++;
           MessageBox.Show("Test 2");
           countThree++;
       }





关闭第一个MessageBox之后,代码一直持续到第二个MessageBox之前,然后循环回到方法的开头。这一直持续到应用程序崩溃或我停止调试。 MessageBox控件和任何其他Modal表单似乎都会出现此问题。如果根本没有MessageBox或Modal表单,那么代码就像正常一样执行。



什么会导致我的应用程序,以及我写的上面的示例代码这样做?即使我使用FormClosed事件而不是FormClosing,问题仍然存在。如果我处理Form1上的Deactivate事件并将代码放在那里,它似乎工作正常,但如果手动关闭弹出窗体(例如单击X按钮),则问题仍然存在。 br />


感谢您的时间和任何帮助。



After closing the first MessageBox the code continues on until right before the second MessageBox and then loops back to the beginning of the method. This continues until either the application crashes or I stop debugging. This problem seems to happen with the MessageBox control and any other Modal forms. If there is no MessageBox or Modal form at all, then the code executes like normal.

What would be causing my application, and the above example code that I wrote to behave this way? The problem persists even if I use the "FormClosed" event instead of "FormClosing". If I handle the "Deactivate" event on "Form1" and place my code there instead, it seems to work fine, but then if the popup form is closed manually (ex. clicking the "X" button) then the problem continues.

Thanks for your time and any help you can provide.

推荐答案

我认为写出这一切有帮助我找到了解决问题的方法。



由于某些原因,我不确定原因,每次第一个MessageBox关闭时都会调用popup forms deactivate事件。这导致了我的无限循环。



解决方案是在触发FormClosing事件时从弹出窗体中删除deactivate事件。



I think writing all this out helped me find the a solution to my problem.

For some reason that I'm not sure why, the popup forms deactivate event was called each time the first MessageBox was closed. This resulted in my endless loop.

The solution was to remove the deactivate event from the popup form when its "FormClosing" event was triggered.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class frmPopup : Form
    {

        public frmPopup()
        {
            InitializeComponent();

            //Events
            this.Deactivate += frmPopup_Deactivate;
            this.FormClosing += frmPopup_FormClosing;
        }

        void frmPopup_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Deactivate -= frmPopup_Deactivate; //Remove the Deactivate event now that the form is closing
        }

        void frmPopup_Deactivate(object sender, EventArgs e)
        {
            this.Close();
        }
              
    }
}


这篇关于表单结束事件保持循环MessageBox [已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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