MDI Dialogs Problem Closing (Important)

otherside

Centurion
Joined
Mar 16, 2003
Messages
127
Location
UK - Greece
Ok guys here my problem and it's 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 it's not inside an mdi parent
but when it does like this example i get the error

Can't call Close() while doing CreateHandle()

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

And another one why i can't 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 ?
 
Your first problem is that you can't call Close within the Load event. You can try moving the code to your constructor, but I'd rethink your strategy on forms first. For example, maybe the MDI form should be showing Form3 and if it's not Cancel THEN show Form2.

If you set the MDIParent property, you're telling Windows that this will be a child window, to be placed "inside" of an MDI Parent. It can't be inside AND be a modal window. Basically, if you're a child window, set the MdiParent property to the parent form and call Show. If you're modal, don't 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 it's 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 it's kind of optional which makes it a bit comlicated. Anyway thanks again and any other ideas are welcome.
 
Back
Top