1
votes

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.

3
UI like that drives me nuts. Yes, I'm sure dammit.Hans Passant
What if you realize you weren't that sure after it closes hahaTheveloper
Then I say "shoot" and start it back up, my mistake. That's one shoot for every hundred dammits.Hans Passant
but you don't agree that a bit of discomfort can save a lot of pain?Theveloper
Why on Earth would closing a program cause 'a lot of pain'? I close programs all the time, it never hurts.Hans Passant

3 Answers

0
votes
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

0
votes

Start form from parent

    frmMdiChild1.MdiParent = Me
    frmMdiChild1.Show()

Add Formclosing Sub

Private Sub frmMdiChild1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.UserClosing Then
        e.Cancel = True
    End If
End Sub

@Theveloper: I tried .MdiFormClosing but it didn't work. To find out what to use, I did a MsgBox(e.CloseReason). Also, only e.Cancel = True will also prevent parent from closing(lol).

(vb.net 2010)

0
votes

Public Class clsGlobalVariables

Public Shared mdi_main As mdiMain

End Class

Imports System.Windows.Forms

Public Class mdiMain

Public forced_close As Boolean = False

Private Sub mdiMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    e.Cancel = True
    If MsgBox("Are you sure you want to exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        ' Close all child forms of the parent.
        For Each ChildForm As Form In Me.MdiChildren
            ChildForm.Close()
        Next
        e.Cancel = False
    End If
End Sub

End Class

Public Class frmMember

Private Sub frmMember_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    Select Case e.CloseReason
        Case CloseReason.UserClosing
            e.Cancel = True
            If Not clsGlobalVariables.mdi_main.forced_close Then
                If MsgBox("Are you sure you want to close?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    e.Cancel = False
                End If
            End If
        Case Else
            clsGlobalVariables.mdi_main.forced_close = True
            e.Cancel = True
    End Select
End Sub

End Class