MDI Dialogs Problem Closing (Important)

otherside

Well-known member
Joined
Mar 16, 2003
Messages
127
Location
UK - Greece
Ok guys here my problem and its getting annoying.

The MDI Parent form

Code:
Private Sub MenuItem2_Click
Dim ma As New Form2()
ma.MdiParent = Me
ma.Show()
End Sub

The Form2
Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim ha As New Form3()
        If ha.ShowDialog() = DialogResult.Cancel Then
            Me.Close()
        End If

End Sub

This works fine when its not inside an mdi parent
but when it does like this example i get the error

Cant call Close() while doing CreateHandle()

Any hints ? any Ideas why is this happening ? and also any solutions ?

And another one why i cant the the mdiparent to a dialog? i get the exception:

Forms that are not top level forms cannot be displayed as a modal dialog. Remove the form from any parent form before calling showDialog.

I understand what is says but why ?
is there any way to override this ?
 
Why dont you just not even load the MDI child unless ShowDialog returns ok?
 
Your first problem is that you cant call Close within the Load event. You can try moving the code to your constructor, but Id rethink your strategy on forms first. For example, maybe the MDI form should be showing Form3 and if its not Cancel THEN show Form2.

If you set the MDIParent property, youre telling Windows that this will be a child window, to be placed "inside" of an MDI Parent. It cant be inside AND be a modal window. Basically, if youre a child window, set the MdiParent property to the parent form and call Show. If youre modal, dont set MdiParent (or set it to Nothing if you already set it to a form), then call ShowDialog.

-Nerseus
 
thanks guys, the thing is that in this project i have has about 60 forms and dialogs, and its quite confusing to change the call for them, besides some of the forms have the dialog in the load in case the value that is needed is not passed from the pervious form, so its kind of optional which makes it a bit comlicated. Anyway thanks again and any other ideas are welcome.
 
Back
Top