2

I have two forms and I have linked it up so that when the second form is closed the first is closed too (using the FormClosing method).

The problem with this is that when I wish to hide the second form it automatically closes the first. Is there a way in which a form can be hidden without actually calling the FormClosing method?

The FormClosing method still seems to be called when "Visible = false" and "Hide()" are used.

Thanks.

Jpin
  • 1,527
  • 5
  • 18
  • 27

5 Answers5

2

call Hide() on the form or Visible=false, but onsider in this case the the form remains in memory and all allocated resources by that form remain in the memory.

If this is a problem for you, I would suggest to revise your architecture.

Tigran
  • 61,654
  • 8
  • 86
  • 123
2

I changed my program so that it is started as below:

        MainForm mainForm = new MainForm();
        mainForm.Show();
        Application.Run();

Instead of:

        Application.Run(new MainForm());

In each of the forms I have added a FormClosing event which checks to see if the user has opted to close the application. If this is the case a prompt is shown to the user to ask for their confirmation:

    private void ImageSelect_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            if (DialogResult.No == MessageBox.Show("Are you sure you wish to exit?", "Exit Confirmation", MessageBoxButtons.YesNo))
                e.Cancel = true;
            else { Application.Exit(); }
        }
    }

The application now can be closed from any form in the application.

Jpin
  • 1,527
  • 5
  • 18
  • 27
0

You want to use form.Hide().

This just conceals the form from the user without actually closing it.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
0
this.Hide();

or

this.Visible = false;
Philip Badilla
  • 1,038
  • 1
  • 10
  • 21
-1

If you use WinForms forms the method Hide() should be the one which you are looking for.

brgerner
  • 4,287
  • 4
  • 23
  • 38
  • when I use Hide() it still calls the FormClosing method. – Jpin Feb 15 '12 at 12:36
  • Maybe you can use property FormClosingEventArgs.CloseReason (https://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason%28v=vs.110%29.aspx) within _FormClosing_ method to decide hwat to do. – brgerner Apr 19 '16 at 11:06