Search code examples
vb.netmdiformclosing

VB.NET Prevent MDI Parent FormClosing from Closing MDI Child before check


I'm trying to give the user a chance to cancel FormClosing of MDI Parent by throwing a "Are you sure?" msgbox with an e.cancel however when FormClosing is invoked by MDI Parent, all MDI Children close first, before the msgbox appears.

I was wondering if there's an easier way to prevent MDI Children from closing rather than having to e.cancel every child form's FormClosing until I get a positive response to close and then push all the close events on the children because that seems like too much of a hassle if you have a lot of MDI Children.

EDIT: I guess the only solution I can find is adding If e.CloseReason = CloseReason.MdiFormClosing Then e.Cancel = True to the FormClosing event and using ApplicationExit instead.


Solution

  • If e.CloseReason = CloseReason.MdiFormClosing Then
       e.Cancel = True
    End If
    

    Should do the job after which you can use Application.Exit whenever you wanna close the application