Visual Basic Tip: 12/25/95


Center a Form Relative to a Main Form.
MERRY CHRISTMAS!


Rather than using the simple Center a form on screen (listed below):
 form1.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2

Try the CenterFRM subroutine listed below to center a form relative
to a main form.  The code also makes sure that the form doesn't go
off the screen.


'call to center a form relative to frmMain
'put in form_load subroutine

 CenterFRM frmChild, Width, Height


'global module routine

Sub CenterFRM(IT As Form, ByVal Wid, ByVal hgt)
 xleft = (frmMain.Width - Wid) \ 2 + frmMain.Left
 
 If xleft < 0 Then
  xleft = 0
 ElseIf xleft + Wid > Screen.Width Then
  xleft = Screen.Width - Wid
 End If

 xtop = (frmMain.Height - hgt) \ 2 + frmMain.Top
 If xtop < 0 Then
  xtop = 0
 ElseIf xtop + hgt > Screen.Height Then
  xtop = Screen.Height - hgt
 End If

 IT.Move xleft, xtop
End Sub

Return to Phil's Web Page

Created by: Philip