|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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
Public Sub
CentreFormInParent(cfrm As Form, pfrm As Form) |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |