Center an MDI Child Form Within the Parent

By | 2002-06-01

useful when you need to center an MDI child form within the parent windo

Original Author: Chris Gibbs

Inputs

The SUB (CenterChild) requires two arguments. The first of these two arguments is the name of the MDI (parent) form. The second argument is the name of the MDI Child form.

Code

Sub CenterChild (Parent As Form, Child As Form)
  Dim iTop As Integer
  Dim iLeft As Integer
  If Parent.WindowState <> 0 Then Exit Sub
  iTop = ((Parent.Height - Child.Height) 2)
  iLeft = ((Parent.Width - Child.Width) 2)
  Child.Move iLeft, iTop ' (This is more efficient than setting Top and Left properties)
End Sub


The next thing you will need to do is actually call the CenterChild procedure. I have placed the call to CenterChild within the child window's Form_Click event procedure.

Sub Form_Click ()
  CenterChild MDIForm1, Form1
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.