-1

Good morning guys.

I have a little problem. If I close my Mainform (Windows-App) when my second form is opened, the second form closes without using my secondForm_FormClosing Event.

Anyone have a idea to handle this?

secondForm_FormClosing

1 Answers1

0

The 2nd form is an Instance of a Form/Class itself so unless you have it setup as an MDI application the 2nd form won't receive the Closing or Closed event because the program is exiting.

You can tap into the 2nd Forms FormClosing event from Form1's closing event, this is one of many ways:

public Form1()
{
    InitializeComponent();
    frm2.FormClosing += new FormClosingEventHandler(form2_FormClosing);
    frm2.Show();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    form2_FormClosing(null, e);
}

private void form2_FormClosing(object? sender, FormClosingEventArgs e)
{
   // This will only get called "directly" when the second form is 
   // closed otherwise it will always be called from Form1_FormClosing.
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321