Visual Basic FAQ

Pure VB: Centering a Form or Dialog in its Parent Form
     
Posted:   Thursday December 26, 1996
Updated:   Monday December 26, 2011
     
Applies to:   VB2, VB3, VB4-16, VB4-32, VB5, VB6
     
 Prerequisites
Network or DUN connection.

This code is probably the second thing a VB developer asks (the first being how to centre on a screen). Just place the CentreFormInParent routine into a BAS module subroutine declared Public so it's available to any form, then call it from the "child" form by passing the child form name and the parent form to position it within.
 BAS Module Code
Place the following code into the general declarations area of a bas module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub CentreFormInParent(cfrm As Form, pfrm As Form)
    
   'cfrm=child form
   'pfrm=parent form 
    cfrm.Move pfrm.Left + (pfrm.Width - cfrm.Width) / 2, _
              pfrm.Top + (pfrm.Height - cfrm.Height) / 2


End Sub
 Form Code
In any form to center within another, place this into the form load, substituting the real parent form name:

Private Sub Form_Load()
   
  'Called from within the child form. Pass 
  'a reference Me and the name of the 
  ''parent' form to centre within
   CentreFormInParent Me, parent_form_name 

End Sub
 Comments
Often you will see the recommendation that to centre a form you should set the form's left and top properties as in:

      Me.Left = Screen.Width / 2
      Me.Top = Screen.Height / 2

While this method is certainly not incorrect, its execution involves two commands, and, on a slower system or one without accelerated video, the user might see the form shift position first horizontally as the '.Left =' code is executed, then vertically as the '.Top =' code is executed.  The Move command performs both horizontal and vertical repositioning together in one move.  For the record, the above one-liner method spelled out is:

Public Sub CentreFormInParent(cfrm As Form, pfrm As Form)

    Dim childLeft As Long
    Dim childTop As Long
    Dim parentLeft As Long
    Dim parentTop As Long

    parentLeft = pfrm.Left
    parentTop = pfrm.Top
    childLeft = parentLeft + ((pfrm.Width - cfrm.Width) / 2)
    childTop = parentTop + ((pfrm.Height - cfrm.Height) / 2)

    cfrm.Move childLeft, childTop

End Sub


 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter