Abount FormClosing Event

Hai Le 1 Reputation point
2020-09-10T09:01:48.773+00:00

Hi MSDN member
i have a question with FormClosing event in C#

I create project which have two form (FormMain and subform )
so in FormMain showdialog subform
in SubForm when I run subForm.Hide it raise closing event (Only the first time)

Why when run form hide comand Closing event run too ( Only the first time, second time and third time closing event not run) it is correct ?
When I set e.Cancel = true in closing event every time closing event run
Thanks you for your support !

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,860 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sheng Jiang 蒋晟 206 Reputation points
    2020-09-12T18:45:42.37+00:00

    Modal Forms you created with ShowDialog are intended to be reused so they are not closed when you call Hide or click on the close button. See the documentation of ShowDialog.

    You should not try Hide your modal form. ShowDialog makes the rest of application unreachable and now you hide the only part that is left available to the user, what are you trying to accomplish? .Net is nice enough to prevent you from shooting yourself on the foot, but as a result the code is not really enjoyable for reading. If you want to hide the form you should set DialogResult, maybe along with other data properties, so the caller of ShowDialog knows what the user did on the form and can decide what to do next.

    0 comments No comments