Search code examples
vb.netwinformsformclosing

Close window confirmation on mdi parent without closing child


I have a mdi parent form which when open may have 4+ child windows open everything is operating great except the Close function - I have a confirmation window show as follows

Private Sub index_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If MessageBox.Show("Are you sure you want to exit?", "prompt", MessageBoxButtons.YesNo) = DialogResult.Yes Then
        'allow close
    Else
        e.Cancel = True
    End If
End Sub

this is working appropriately EXCEPT when you click no while the mdi parent remains open the child forms all close. How do I prevent this?

Images to help:

Normal operation http://fasttrackdispatch.com/design/normalop.png

but when you click x and it brings up the prompt message which looks like this (notice for some reason all mdi child elements closed - I did not code for this to happen) http://fasttrackdispatch.com/design/exitmenu.png When you click no the mdi children do not return http://fasttrackdispatch.com/design/exitmenuno.png

the question is why are the mdi children closing and how can we prevent it?


Solution

  • You could handle the FormClosing event of the child form(s) and, if e.CloseReason is MdiFormClosing, you can then prompt the user to confirm before the child form(s) is closed.