Opening non modal forms inside of an MDI parent

Working with MDI applications in WinForms is straightforward—right up until you try to center a child form. Developers often assume that setting StartPosition = CenterParent will place the form in the middle of the MDI container, but MDI children don’t follow the same rules as modal dialogs. Instead, they ignore the setting entirely and appear in the top-left corner. In this article, we’ll walk through a simple, reusable pattern for opening MDI child forms centered inside the parent window, along with clean VB.NET examples you can drop into your project.

Why MDI Child Forms Ignore CenterParent

FormStartPosition.CenterParent only works for modal dialogs (ShowDialog). MDI children are hosted inside an internal MdiClient control, and Windows does not apply parent-based centering logic to them. That means you must manually calculate the position before showing the form.

MDI Parent Code: Opening a Centered Child Form

The OpenForm method assigns the MDI parent, switches to manual positioning, and calculates the centered coordinates:

Imports System.Windows.Forms
 
Public Class MDIParent1
 
    Public Sub OpenForm(frm As Form)
        ' Opens a child form centered inside the MDI parent.
        ' MDI children ignore StartPosition.CenterParent, so we must position manually.
 
        ' Assign this form as the MDI parent
        frm.MdiParent = Me
 
        ' Required so our manual Left/Top values are used
        frm.StartPosition = FormStartPosition.Manual
 
        ' Center the child form inside the MDI parent window.
        ' Note: Using Me.Width/Height centers relative to the full MDI form,
        ' not the MdiClient area, but works well for most layouts.
        frm.Left = (Me.Width - frm.Width) \ 2
        frm.Top = (Me.Height - frm.Height) \ 2
 
        ' Display the form
        frm.Show()
    End Sub
 
    ' Runs when the MDI parent loads
    Private Sub MDIParent1_Load(sender As Object, e As EventArgs) Handles Me.Load
 
        ' Store a reference globally if other forms need access to the MDI parent
        Globals.MDIParent = Me
 
        ' Create the initial child form and open it centered
        Dim frm As New Form1
        OpenForm(frm)
    End Sub
 
End Class

This gives you a consistent, predictable way to open any child form in the center of the MDI window.

Child Form Code: Opening Additional Forms

Child forms can open other child forms by retrieving the MDI parent reference and calling OpenForm again:

Public Class Form1
 
    ' Holds a reference to the MDI parent so this form
    ' can open additional child forms through it.
    Dim mfrmParent As MDIParent1
 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
 
        ' Retrieve the MDI parent instance that was stored globally.
        ' This avoids needing to walk the form hierarchy to find the parent.
        mfrmParent = CType(Globals.MDIParent, MDIParent1)
 
    End Sub
 
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 
        ' Create a new child form instance
        Dim frm As New Form2
 
        ' Ask the MDI parent to open it using the same centering logic
        mfrmParent.OpenForm(frm)
 
    End Sub
 
End Class

This keeps all child-form creation consistent and avoids duplicating centering logic across multiple forms.

Wrapping Up

MDI applications still have their place in line-of-business software, but they come with quirks—like ignoring CenterParent. By centralizing your child-form creation in a single OpenForm method, you get predictable behavior, cleaner code, and a reusable pattern that works across your entire application. Whether you're opening the first form on startup or launching additional forms from inside other children, this approach keeps everything centered and consistent.

 

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences