Winforms CloseReason=WindowsShutDown in MdiChild Form

1 Answer 39 Views
Form
Matthias
Top achievements
Rank 1
Matthias asked on 11 Sep 2024, 01:10 PM | edited on 11 Sep 2024, 01:30 PM

Hi!

I have MdiParent Form which hosts multiple MdiChids. I would like to determine why the MidParent is being closed withing the MdiChild-ClosingEvent. The CloseReason in the MdiChild-ClosingEvent only says "MdiFormClosing". But I dont know if the MdiParent is closed due to user operation (eg. pressing X in upper right corner) or Windows is being shutdown.

How can I solve that?


background:

The MdiChild_FormClosing-Event either asks if this or all childs shall be closed. If all are confirmed, the static variable of "bool _ForceClosing" prevents the next child from asking again. So good so far. I would like to skip this confirmation stuff in case, windows is being shutdown. But this CloseReason in MdiChild only has UserClosing or MdiFormClosing.

System::Void 
Form_Client::Form_Client_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e)
  {
    //-----------------------------------------------------------------------------------------------------------------------------
    try
    {
      //-- mdi closing already confirmed --------------------------------------------------------------------
      if (this->_ForceClosing == true)
      {
        e->Cancel = false;
        return;
      }
      //-- windows shutdown ---------------------------------------------------------------------------------
      else if (e->CloseReason == System::Windows::Forms::CloseReason::WindowsShutDown)  // not working
      {
        this->_ForceClosing = true;
      }
      //-- user single closing (ask if connected) -----------------------------------------------------------
      else if (e->CloseReason == System::Windows::Forms::CloseReason::UserClosing)
      {
        // ask if this child shall be closed
        if (Result != System::Windows::Forms::DialogResult::OK)
        {
          e->Cancel = true;
          return;
        }
      }
      //-- mdi parent closing initiated ---------------------------------------------------------------------
      else if (e->CloseReason == System::Windows::Forms::CloseReason::MdiFormClosing)
      {
        // ask if all childs shall be closed
        if (Result != System::Windows::Forms::DialogResult::Yes)
        {
          e->Cancel = true;
          return;
        }
        //-------------------------------------------------------------------------------
        this->_ForceClosing = true;
      }
      //-- unknown reason -> just close all -----------------------------------------------------------------
      else
      {
        this->_ForceClosing = true;
      }
    }
    //----------------------------------------------------------------------------------------------------------------------------
    catch (System::Exception^ ex)
    {
      System::Windows::Forms::MessageBox::Show( ex->ToString() ); 
    }
  }
Thanks in advance

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 16 Sep 2024, 12:11 PM

Hello Matthias,

I appreciate the provided information.

I have checked the reported behavior and in a WinForms application, the FormClosing event won't be called when the application closes. I have tried different approaches to kill the application but the closing event of the form is not called. No matter if it is an Mdi child or not. In general, to catch the moment when the application exits, you can subscribe to the Application.ApplicationExit event. Can you specify which platform you are using for your application development process?

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Matthias
Top achievements
Rank 1
commented on 23 Sep 2024, 08:49 AM

Hi. THanks for responding.



No, this events are obviously being fired when shutting down windows:

mdi_child.FormClosing with MdiFormClosing as reason
(other childs)

then
mdi_parent.FormClosing with WindowsShutdown (I assume this is fired too)


But I wanna know during the child.FormClosing event, that the reason, the parent form is being closed, is either UserClosing oder WindowsShutdown. But how? This informations is only available after all childs are already closed in parent-FormClosing event.
Dinko | Tech Support Engineer
Telerik team
commented on 26 Sep 2024, 08:05 AM

I think I am not debugging it correctly. Can you check my test project and modify it to mimic your structure? You can find the project attached to this reply. When you run the project, click on the button to open a RadForm and then click on the menu items to add MDI child. I have subscribed to several events but none of them is triggered when I stop the application from VS, which I think simulates closing the application from the OS system (windows shutdown). May I ask you to check the project and share some steps which I need to follow to get closer to your case and I will try to find a solution?
Tags
Form
Asked by
Matthias
Top achievements
Rank 1
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or