You are on page 1of 2

Center A Form

Here's how to center a form so it will appear in the middle of the screen, put this
in a button:

Me.Top = (Screen.Height - Me.Height) / 2


Me.Left = (Screen.Width - Me.Width) / 2

Resizing a Form (an easy way)


This code lets you resize your forms with a neat stretch effect. Copy and paste the
code as directed by the comments:

'-----------------------------------------------------
' Put this code in the public area of your form (ie, the top most part)
'-----------------------------------------------------

Private Type ScaleStruct


Top As Integer
Left As Integer
Width As Integer
Height As Integer
ParentHeight As Integer
ParentWidth As Integer
FontSize As Integer
End Type

Dim Ctrl() As ScaleStruct


Dim minWidth As Integer
Dim minHeight As Integer
Dim maxWidth As Integer
Dim maxHeight As Integer

'-----------------------------------------------------
' Put this code in the Form_Load event
'-----------------------------------------------------

On Error Resume Next

Dim i As Integer
ReDim Ctrl(0 To Me.Controls.Count - 1)

Me.ScaleMode = 3

For i = 0 To Me.Controls.Count - 1
Ctrl(i).Top = Me.Controls(i).Top
Ctrl(i).Left = Me.Controls(i).Left
Ctrl(i).Width = Me.Controls(i).Width
Ctrl(i).Height = Me.Controls(i).Height
Ctrl(i).ParentHeight = Me.Controls(i).Parent.ScaleHeight
Ctrl(i).ParentWidth = Me.Controls(i).Parent.ScaleWidth
Ctrl(i).FontSize = Me.Controls(i).FontSize
Next

' THESE VALUES ARE ARBITRARY


' Change them to best suit your program
' One tip would be to have a min size, but not a real
' max size (ie, make the maxes larger than what the
' screen size will ever be
minWidth = 400
minHeight = 400
maxWidth = 800
maxHeight = 800

'-----------------------------------------------------
' Put this code in the Form_Resize event
'-----------------------------------------------------

On Error Resume Next

Dim i As Integer
Dim ParentSH As Integer, ParentSW As Integer

ParentSH = Me.Controls(i).Parent.ScaleHeight
ParentSW = Me.Controls(i).Parent.ScaleWidth

For i = 0 To Me.Controls.Count - 1
If Me.ScaleHeight >= minHeight And Me.ScaleHeight <= maxHeight Then
Me.Controls(i).Top = Ctrl(i).Top * (ParentSH / Ctrl(i).ParentHeight)
Me.Controls(i).Height = Ctrl(i).Height * (ParentSH / Ctrl(i).ParentHeight)
Me.Controls(i).FontSize = Ctrl(i).FontSize * (ParentSH /
Ctrl(i).ParentHeight)
If Me.Controls(i).FontSize < 8 Then Me.Controls(i).FontSize = 8
If Me.Controls(i).FontSize > 12 Then Me.Controls(i).FontSize = 12
End If

If Me.ScaleWidth >= minWidth And Me.ScaleWidth <= maxWidth Then


Me.Controls(i).Left = Ctrl(i).Left * (ParentSW / Ctrl(i).ParentWidth)
Me.Controls(i).Width = Ctrl(i).Width * (ParentSW / Ctrl(i).ParentWidth)
End If
Next

You might also like