How to center controls

Hello

How can I center controls in the form

I know how to center controls by using "Format-->Center in Form".

But I need a code for centering controls.

 

Thanks



Answer this question

How to center controls

  • tspree15

    Thanks

    It's working now.

    Now it's in the middle exactly.

    Thanks again for everone replied.


  • stigmat

     spotty wrote:

    Something like the following should be what your looking for

       Sub CenterControlOnForm(ByVal Ctrl As Control, ByVal frm As Form)
            Ctrl.Top = (frm.Height - Ctrl.Height) / 2
            Ctrl.Left = (frm.Width - Ctrl.Width) / 2
        End Sub

    It's not working.

    It doesn't center exactly.

    P.S: I'm using VB2005.


  • kathy86

    Try modifing the code to center based on just the size of the client area, rather than the entire window:

    Public Sub CenterControlInForm(ByVal ctrl As Control, Optional ByVal Horizontal As Boolean = True, Optional ByVal Vertical As Boolean = True)

    Dim prnt As Form = ctrl.FindForm

    If Not prnt Is Nothing Then

    If Horizontal Then

    ctrl.Left = (prnt.ClientRectangle.Width / 2) - (ctrl.Width / 2)

    End If

    If Vertical Then

    ctrl.Top = (prnt.ClientRectangle.Height / 2) - (ctrl.Height / 2)

    End If

    End If

    End Sub

    This could also be used as a shared method on an object. It will center the control passed in the client area of it's parent form (if a parent form is available). A value of "False" can be applied to either optional parameter to prevent the code from centering in a specific direction.



  • The Poodle

     cgraus wrote:

    I can't imagine how it could fail to centre exactly, unless you're centering a label, in which case it's possible the label takes up more room than the visible text, and it would seem to be not working. 

    It's much easier to work out what your problem is if you can provide more info than 'it's not working' and perhaps post some code.  At this point, we don't even know what type of controls you're trying to centre

    I want to center buttons.

    Look at this picture: http://www.onh1986.com/vb.jpg

    Button2 was centered by "Format-->Center in Form" and it's in the middle exactly, but Button1 was centered by that code.

    Note the difference.

     


  • chet1940

    I can't imagine how it could fail to centre exactly, unless you're centering a label, in which case it's possible the label takes up more room than the visible text, and it would seem to be not working.

    It's much easier to work out what your problem is if you can provide more info than 'it's not working' and perhaps post some code. At this point, we don't even know what type of controls you're trying to centre



  • Tre4

    I think (0,0) is at the exact corner of the form and the top border (which I think is 38 pixels) isn't taken in account. Try adding 38/2 pixels to the Y-axis and see what happens.



  • teh squirrel

    Something like the following should be what your looking for

    Sub CenterControlOnForm(ByVal Ctrl As Control, ByVal frm As Form)
    Ctrl.Top = (frm.Height - Ctrl.Height) / 2
    Ctrl.Left = (frm.Width - Ctrl.Width) / 2
    End Sub


  • PhilipMorley

    Perhaps one or the other is not taking the non client area into account

    Now that you have some code to work with, perhaps if you set the debugger, you can look at what values it's coming up with and work out why they are different to what you expect



  • How to center controls