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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i want to show all the control in the center of form any resolution

Status
Not open for further replies.

djayakum

Programmer
Feb 2, 2002
28
IN
hello ,

in my application.
if i use 800*600 no problem , if i change to
1024*768 my display area shown in left top
on that sutuation i want to show the all the control
in the center of the form.
i want to show all the control in the center of form
if the user changes the resolution ?

if any one knows please post.

thanks

 
I can't mail you - you have not left an email address...

I'm not sure I entirely understand the question? If you're asking how to centre a control in a form, the answer is

control.top = (form.height - control.height) / 2
control.width = (form.width - control.width) / 2

If on the other hand you are talking about having a form centralised on the windows desktop, use the startupposition of the form... For example:

MyForm.StartupPosition = vbStartUpScreen


Hope that helps,

Martin (mmilan)
 
If resizing each control is not important, drop all of your controls onto a frame and in the resize event of the form center the frame. It should make your life a bit easier.
 
If resizing is important, you can still theoretically write code to handle it so long as you store the form scale details in member variables - so you can see the old settings during the resize event...
 
As long as you do not need to resize the controls (e.g. going to a higher resoloution) the following will at least get you started down A path.



Code:
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 &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

    For Idx = 30 To 41
        MyCtrlName = &quot;Cmd&quot; & Trim(str(Idx))
        Me(MyCtrlName).Caption = &quot;CMMD&quot; & Trim(str(Idx))
    Next Idx

End Sub

MichaelRed
m.red@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