pro_windows_forms thread: How to know application is closed by clicking on the X button of MDI form
> Hi Roger,
I try this in VB .NET, it works great!
Appreciate for your help!
Raymond
Raymond:
> 1. In your MDIParent form, place the following code:
> private bool _allowChildrenToClose = false;
> internal bool AllowChildrenToClose
> {
> get {return this._allowChildrenToClose;}
> }
> protected override void WndProc(ref System.Windows.Forms.Message m)
> {
> const int WM_CLOSE = 0x10;
> if (m.Msg == WM_CLOSE)
> this._allowChildrenToClose = true;
> base.WndProc(ref m);
> }
> 2. In your MDIChild form, place the following code:
> protected override void OnClosing(
> System.ComponentModel.CancelEventArgs e)
> {
> if (((ParentForm) this.MdiParent).AllowChildrenToClose == false)
> e.Cancel = true;
> base.OnClosing(e);
> }
> 3. You will have to change one word in step #2 above. I use a cast
> on the "IF" instruction line to cast "this.MdiParent" to an object
> of type "ParentForm".
> You should substitute whatever type of form your MDIParent form
> is for the cast "ParentForm". So, for instance, if your MDIParent
> form is of type MyMdiParentFormClass, then the line should be
> changed to the following:
> if (((MyMdiParentFormClass) this.MdiParent).AllowChildrenToClose
> == false)
> The above code (step #1) will trap the WM_CLOSE event in your MDIParent
f> orm. This event could be raised by:
> - Clicking the X button in the top-right corner of your parent form.
> - Calling the Close() method on your parent form.
> - etc.
> In short, it is called whenever someone tries to close the parent form
b> y any means.
> The code in step #2 is called whenever anyone tries to close a child
f> orm. It first checks to see whether or not the child is closing due
t> o the parent being closed.
> - If the child is closing because someone clicked the X button in
> the upper right corner of the child form, then the child form
> will remain open.
> - If on the other hand, the child is closing because someone is
> trying to close the MDI parent form, then the child form will close.
> Cheers.
> - Roger Nedel
> Nedel Software Solutions
> rnedel@n...