Launch MDI-Form from MDI-Form

Thei

Newcomer
Joined
Aug 9, 2006
Messages
13
Hi all,

I've got a MDIParent Form (frmMain) from which I launch a MDIChild Form (frmMDI_1).

Now I want to launch the second MDIChild Form (frmMDI_2) from within the first MDIChild Form. The MDIParent of the second MDIChild Form should also be frmMain.

I tried to do this as shown below

Code:
'Inside a Module
Public WithEvents MainForm As frmMain
Public WithEvents Child1 As frmMDI_1 
Public WithEvents Child2 As frmMDI_2

'Inside the frmMDI_1 Form
Child2 = New frmMDI_2

With Child2
    .MdiParent = MainForm
    .Show()
    .WindowState = FormWindowState.Maximized
End With

When the code above is ran the frmMDI_2 Form is not shown as a MDIChild but as a normal form. What am I doing wrong?

Thanks
 
Could you signal the MDIParent Form from the first MDIChild Form with a custom method within the MDIChild?
Visual Basic:
Class frmMain
 
  Public Function ParentShow(nForm as Integer) As Integer
    If (nForm = 1) Then
      If (Child1 Is Nothing) Then
        Child1 = New frmMDI_1
        With Child1
          .MdiParent = MainForm
          .WindowState = ForWindowState.Maximized
          .Show()
         End With
         Return 1 ' Can be interpreted as a True
      Else
        Return 0 ' Can be interpreted as a False
      End If
    Else If (nForm = 2) Then
      If (Child2 Is Nothing) Then
        Child2 = New frmMDI_2
        With Child2
          .MdiParent = MainForm
          .WindowState = ForWindowState.Maximized
          .Show()
         End With
         Return 1 ' Can be interpreted as a True
      Else
        Return 0 ' Can be interpreted as a False
      End If
    Else
      Return -1 ' Signal an error
    End If
  End Sub
 
End Class
Also, you may want to play around with the order that you call Show() and WindowState. I had some problems with them recently where I was attempting to hide an application in the task bar tray, and it turned out I wasn't calling the methods in the right order. It wasn't documented anywhere that I could find, though.

Hope this helps.
 
I tried to use the function that you suggested but, although no error is raised, no form is being displayed.
 
This is very odd. When I made a new project and used code simular to the code above it worked fine. But in my excisting project it still doesn't work.

I've attached the test project to this post.
 

Attachments

Strangely enough that is the case. That's why I don't understand that no form is shown.
 
They seem to be valid.

MDIParent = {PROJECTNAME.frmMain}

When I display a MsgBox after the Show method it also shows the right MDIParent (frmMain).

Code:
With MDI_1
    .MdiParent = Me
    .Show()
    .WindowState = FormWindowState.Maximized

    MsgBox(.MdiParent.Name)
End With
 
Shouldn't you be creating a new instance of the form?
Visual Basic:
MDI_1 = New frmMain()
With MDI_1
  .MdiParent = Me
  .Show()
  .WindowState = FormWindowState.Maximized
  MsgBox(.MdiParent.Name)
End With
 
Shouldn't you be creating a new instance of the form?
Visual Basic:
MDI_1 = New frmMain()
With MDI_1
  .MdiParent = Me
  .Show()
  .WindowState = FormWindowState.Maximized
  MsgBox(.MdiParent.Name)
End With

Yes that's right. I forgot to paste that part into this post.
 
I've found the problem. Before the MDIParent form is being shown the user has to log in. This is done using a loginform.

In the properties of the project I set that loginform as startup form, and I changed the option Shutdown Mode to When Last Form Closes.

After I changed these settings in the test project that I attached to this post no MDI form was shown when the button was clicked.

The code used to show the MainForm from inside the LoginForm is like the one below.

Code:
Dim frm As New frmMain

frm.Show()

Me.Close

Yet when I change that code to this

Code:
frmMain.Show()

Me.Close

it works. Although very happy that it finally works, I'm quite curious to why it didn't work using the first method of showing the MainForm.
 
Back
Top