If you use a higher screen resolution than your users, your forms could be partially hidden or even completely off the screen.
There are a few different ways to center your Forms and this tip is by far the best one. It’s very useful because you can center your form over any other form. It’s good to center status Form over your applications mail form instead of the Screen. It just looks nicer. This object can be another Form or the Screen.
Code
Sub CenterForm(objChild As Object, objParent As _
Object, Optional vLeftTopOffset As Variant, _
Optional vTopOffset As Variant, Optional vMode _
As Variant)
Dim iLeft As Integer
Dim iTop As Integer
Dim iMode As Integer
Dim iLOffset As Integer
Dim iTOffset As Integer
Dim I As Integer
If TypeOf objParent Is SysInfo Then
iLeft = objParent.WorkAreaLeft + _
(objParent.WorkAreaWidth - objChild.Width) / 2
iTop = objParent.WorkAreaTop + _
(objParent.WorkAreaHeight - objChild.Height) / 2
ElseIf TypeOf objParent Is MDIForm Then
If objChild.MDIChild = True Then
iLeft = (objParent.ScaleWidth - _
objChild.Width) / 2
iTop = (objParent.ScaleHeight - _
objChild.Height) / 2
Else
iLeft = objParent.Left + (objParent.Width - _
objChild.Width) / 2
iTop = objParent.Top + (objParent.Height - _
objChild.Height) / 2
End If
ElseIf TypeOf objParent Is Screen Then
iLeft = (objParent.Width - objChild.Width) / 2
iTop = (objParent.Height - objChild.Height) / 2
ElseIf TypeOf objParent Is Form Then
If objParent.MDIChild = True Then
iLeft = objParent.Left + (objParent.Width - _
objChild.Width) / 2
iTop = objParent.Top + (objParent.Height - _
objChild.Height) / 2
For I = 0 To Forms.Count - 1
If TypeOf Forms(I) Is MDIForm Then
iLeft = iLeft + (Forms(I).Width - _
Forms(I).ScaleWidth) / 2 + _
Forms(I).Left
iTop = iTop + (Forms(I).Height - _
Forms(I).ScaleHeight) / 2 + _
Forms(I).Top
Exit For
End If
Next I
Else
iLeft = objParent.Left + (objParent.Width - _
objChild.Width) / 2
iTop = objParent.Top + (objParent.Height - _
objChild.Height) / 2
End If
Else
Exit Sub
End If
If IsMissing(vMode) Or objChild.MDIChild = True Then
iMode = vbModeless
Else
iMode = Int(vMode)
End If
If IsMissing(vLeftTopOffset) Then
iLOffset = 0
Else
iLOffset = Int(vLeftTopOffset)
End If
If IsMissing(vTopOffset) Then
iTOffset = 0
Else
iTOffset = Int(vTopOffset)
End If
objChild.Move iLeft + iLOffset, iTop + iT Offset
objChild.Show iMode
End Sub
Examples
Example 1
Center the Form on the Screen.
CenterForm objChild:=Me, objParent:=Screen
Example 2
Center the Form as Modal on the Screen.
CenterForm objChild:=Me, objParent:=Screen, vMode:=vbModal
Example 3
Center the Form on the Windows 95 viewing area (takes into account the TaskBar). This requires that a SysInfo control (which comes with Visual Basic) be placed on the Form.
CenterForm objChild:=Me, objParent:=SysInfo
Example 4
Centers the Form on a Parent Form.
CenterForm objChild:=Me, objParent:=frmMain
You can also use the vTopOffset and vLeftOffset parameters to center the Form and then add or subtract from the X and Y coordinates.
This tip is reprinted from the VB Tips & Tricks Volume 1 book.
Compatible With Visual Basic 4.0
Parts of this tip were submitted by: C.G. Ouimet
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
