Form.Close won't close the form.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I'm getting a strange behavior. When I open a form using the Form.Show()
method,
Form.Close() doesn't seem to do anything. However, If I display the form
using the Form.ShowDialog() method, then Form.Close() cloeses the form just
fine;

Has anyone else noticed this strange behaviour? Is there any way to bypass
it without resorting to displaying all forms modally?

Thanks you in advance.
 
I guarantee you've got something going on in your code causing this - it's
not a behavior of the framework. Show us some simple code that reproduces
it.

-Chris
 
I've just figured out what causes the problem:

The form that won't close is used by a form that is displayed using the
Form.ShowDialog method.

It seems that if form3 is displayed non modally by form2 that is being
displayed modally by form1, then form3 won't close when calling the
Form.Close method. In fact, the form will refuse to close even if the user
clicks on the "X" button!

You may argue that one should not display non modal from modal forms and you
are probably right. The thing is that the normal framework allows you to do
so and the forms close just fine.
 
There is a way around this problem - although not an elegant one - :

1) Set the Form.MinimizeButton property to true.
2) Instead of calling Form.Close from your code, call Form.Hide.
3) Override the Form.OnDeactivate Method:

protected override void OnDeactivate(EventArgs e)
{
this.Dispose();
}
 
Back
Top