Click here to Skip to main content
15,883,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
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");
           
        }             
              
    }
}


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.

C#
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++;
       }


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.
Posted
Updated 3-Sep-21 22:15pm
v2

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.

C#
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();
        }
              
    }
}
 
Share this answer
 
C#
private void InstructionsGui_FormClosing(object sender, FormClosingEventArgs e)
       {
           if (MessageBox.Show("Are you sure you want to close without saving?", "Close without saving", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
           {
               this.FormClosing -= InstructionsGui_FormClosing;
               this.Close();
           }
           else
               e.Cancel = true;
       }

Don't know if this will help but this worked for me, by just removing the formclosing event from the form and then closing the form
 
Share this answer
 
Comments
Richard Deeming 6-Sep-21 5:30am    
Which is exactly what solution 1 said, six years ago!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900