Private Sub Form_Activate()
'Michael 8/15/2001. To 'Center' all of the controls on a Form
'(does NOT change the relative placement or re-size any control)
Dim MyCtrlHt() As Long
Dim MyCtrlWd() As Long
Dim MyCtrlTop() As Long
Dim MyCtrlLft() As Long
Dim TopMin As Long
Dim TopMax As Long
Dim LftMin As Long
Dim LftMax As Long
Dim XOff As Long
Dim YOff As Long
ReDim MyCtrlHt(Me.Controls.Count)
ReDim MyCtrlWd(Me.Controls.Count)
ReDim MyCtrlTop(Me.Controls.Count)
ReDim MyCtrlLft(Me.Controls.Count)
Dim FrmHt As Long
Dim FrmWd As Long
FrmHt = Me.WindowHeight
FrmWd = Me.WindowWidth
TopMin = FrmHt
TopMax = 0
LftMin = FrmWd
LftMax = 0
Idx = 0
'This collects the individual control properties and defines the box
For Each MyCtrl In Me.Controls
MyCtrlHt(Idx) = MyCtrl.height
MyCtrlWd(Idx) = MyCtrl.Width
MyCtrlTop(Idx) = MyCtrl.Top
MyCtrlLft(Idx) = MyCtrl.Left
If (MyCtrlTop(Idx) < TopMin) Then
TopMin = MyCtrlTop(Idx)
End If
If (MyCtrlTop(Idx) > TopMax) Then
TopMax = MyCtrlTop(Idx) + MyCtrlHt(Idx)
End If
If (MyCtrlLft(Idx) < LftMin) Then
LftMin = MyCtrlLft(Idx)
End If
If (MyCtrlLft(Idx) > LftMax) Then
LftMax = MyCtrlLft(Idx) + MyCtrlWd(Idx)
End If
Idx = Idx + 1
Next MyCtrl
XOff = (FrmWd - (LftMax + LftMin)) / 2
YOff = (FrmHt - (TopMax + TopMin)) / 2
'Now we need to Move the various controls to their "Centered" position
For Idx = 0 To Me.Controls.Count - 1
Me.Controls(Idx).Left = Me.Controls(Idx).Left + XOff
Me.Controls(Idx).Top = Me.Controls(Idx).Top + YOff
Next Idx
For Idx = 30 To 41
MyCtrlName = "Cmd" & Trim(str(Idx))
Me(MyCtrlName).Caption = "CMMD" & Trim(str(Idx))
Next Idx
End Sub