Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

centering objects on form 1

Status
Not open for further replies.

striker73

MIS
Jun 7, 2001
376
US
I have a switchboard form that I want to open up maximized. I also want the contents of the form to be pretty much in the center of the form. I've been thinking I put them all in a hidden frame and some how center that. I'm not sure what size resolution the user is going to viewing at, so I want to be able to adjust for 800x600 and 1024x768, and still have the contents fairly centered. Any ideas? Thanks!
 
The resoloution should not be a problem. Setting up a process to "center" a single object is "demonstrated" in the following bit of code, using the Frame as the object to center. However controls within the frame will NOT move with it. It would not be difficult to "programattically" arrange (e.g. move) the other objects "in" the frame to positions in the frame - but it would equally pratical to just locate the individual controls to a specific position (or offset) within the overall form as to do so for the frame.

Code:
Private Sub Form_Activate()

    Dim FrmHt As Long
    Dim FrmWd As Long

    Dim SwBdHt As Long
    Dim SwBdWd As Long

    Dim SwBdTop As Long
    Dim SwBdLeft As Long

    FrmHt = Me.WindowHeight
    FrmWd = Me.WindowWidth

    SwBdHt = Me.Frame10.height
    SwBdWd = Me.Frame10.Width

    SwBdLeft = (FrmWd - SwBdWd) / 2
    SwBdTop = (FrmHt - SwBdHt) / 2
    
    Me.Frame10.left = SwBdLeft
    Me.Frame10.Top = SwBdTop

End Sub


This procedure show a method of "centering" all of the controls on a form.

Code:
Private Sub Form_Activate()

    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 &quot;Centered&quot; 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

End Sub
MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top