Reputation: 874
I have created an app in VB.Net. A child form is displayed using the Showdialog() method so that the parent cannot be accessed until the child is closed.
I need the child form to be disposed every time it is closed so that the information it contains is removed: For this I have put, "Me.Dispose()" in the closing event of the child form.
However, often (not always) when the child is closed the parent is minimized, which is not what I want.
Does anyone know of any way to stop that from happening? All help is appreciated.
Upvotes: 0
Views: 1531
Reputation: 941218
However, often (not always) when the child is closed the parent is minimized
It is not being minimized, it disappears behind the window of another app. Most typically Visual Studio when you are debugging. What goes wrong here is that you destroy the form too early, before Winforms had a chance to re-enable your main window. The Windows window manager is now forced to find another window to give to focus to. None of the windows in your app qualify since they are still disabled so it picks the window of another app. If that window is big enough (the "not always" case) then it will overlap yours and make it disappear, making you think it got minimized.
You solve this by doing it the proper way, disposing the dialog after ShowDialog returns. Always use the Using statement to do so. The boiler-plate code is:
Using dlg = New YourDialogFormClass
If dlg.ShowDialog() = DialogResult.Ok Then
'' Use the dialog results
''...
End If
End Using '' <=== It gets disposed here
Upvotes: 1
Reputation: 2546
A better way to do it is to wrap your modal window in a using statement rather than using Me.Dispose in the closing event of the form. For example, in your parent form do this:
Using frm As New Form1
frm.ShowDialog()
End Using
Upvotes: 2