BUG: form closed & closing event

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

I am having a problem with raising the Form.Close and
Form.Closing event. This only occurs if I don't call
Form.Show() before a call to Form.Close(). Is this by
design?

MyForm f = new MyForm();
f.MdiParent = this;
if (f.SomeFunction() == false)
f.Close(); <--- Closed event does not fire
else
f.Show();

....

if (f!=null)
f.Close(); <--- Closed event fires


MyForm is derived from Form and subscribes to its
Form.Closed event.

I am running 1.1 of the framework.

Nick
 
Showing a form with ShowDialog and Show are different mechanisms.

A non-modal form may be dismissed with Form.Close but the modal (dialog)
form must be dismissed by setting it's DialogResult property so that the
user's choice can be returned to the calling code.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Blog http://bobpowelldotnet.blogspot.com
 
Thanks. I understand that, but the problem I am having is
that the Form.Closed and Form.Closing EVENTS are not being
raised if the Form.Show() METHOD is never called. I
regiser for these event in the form's constructor, which
is being called.

Not sure how this relates with ShowDialog and
DialogResult. Please be a little more specific.
 
I think I remember seeing this mentioned before in the newsgroups. I did a
quick search but couldn't find the original post, so I built a quick test
project. It confirmed this behavior -- if you never Show() the form, then
neither the Closing nor Closed events will fire.

If you Dispose() your form when you're done with it (and if you're not, then
you should), then the Disposed event does fire, even if the form was never
shown. This might be a viable workaround for you.
 
Back
Top