Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Center controls at startup


evilbunnie
 Share

Recommended Posts

Hi there,

How would I center a control on a form at startup,
I've got this far, but it isn't very accurate (I'm not very good at math).

```
'Created by Evibunnie/Captain Wabbit
Sub CenterControl(TheControl as Control, TheForm as Form, Horizontally as Boolean, Vertically as Boolean)

    'Horizontally
    If Horizontally = True Then TheControl.Left = (TheForm.Width / 2) - (TheControl.Width / 2)

    'Vertically
    If Vertically = True Then TheControl.Top = (TheForm.Height / 2) - (TheControl.Height / 2)

End Sub

```
Thanks in advance.
-Evilbunnie
Link to comment
Share on other sites

The maths is fine, you're just not programming it properly. I suggest you go read up on the basics of VB6.

```
Public Sub CentreControl(ByRef theControl as Control, ByRef theForm as Form, Optional ByVal Horizontally as Boolean = False, Optional ByVal Vertically as Boolean = False)

    If Horizontally Then theControl.Left = (theForm.Width \ 2) - (theControl.Width \ 2)
    If Vertically Then theControl.Top = (theForm.Height \ 2) - (theControl.Height \ 2)

End Sub

```
Link to comment
Share on other sites

@Rainbow:

> The maths is fine, you're just not programming it properly. I suggest you go read up on the basics of VB6.
>
> ```
> Public Sub CentreControl(ByRef theControl as Control, ByRef theForm as Form, Optional ByVal Horizontally as Boolean = False, Optional ByVal Vertically as Boolean = False)
>
>     If Horizontally Then theControl.Left = (theForm.Width \ 2) - (theControl.Width \ 2)
>     If Vertically Then theControl.Top = (theForm.Height \ 2) - (theControl.Height \ 2)
>
> End Sub
>
> ```

Robin, that code does the exactly the same thing. Both subs move the control to the same position, but it's not actually in the middle. :3

[See attached for example]

Thanks for your help anyways :)
Link to comment
Share on other sites

My code works fine, you've just set up your project wrong. Change the Form2's 'ScaleMode' to '3 - Pixel'.

Replace the code with:
```
Public Sub CentreControl(ByRef theControl As Control, ByRef theForm As Form, Optional ByVal Horizontally As Boolean = False, Optional ByVal Vertically As Boolean = False)

    If Horizontally Then theControl.Left = (theForm.ScaleWidth \ 2) - (theControl.Width \ 2)
    If Vertically Then theControl.Top = (theForm.ScaleHeight \ 2) - (theControl.Height \ 2)

End Sub

```
Link to comment
Share on other sites

@Rainbow:

> My code works fine, you've just set up your project wrong. Change the Form2's 'ScaleMode' to '3 - Pixel'.
>
> Replace the code with:
> ```
> Public Sub CentreControl(ByRef theControl As Control, ByRef theForm As Form, Optional ByVal Horizontally As Boolean = False, Optional ByVal Vertically As Boolean = False)
>
>     If Horizontally Then theControl.Left = (theForm.ScaleWidth \ 2) - (theControl.Width \ 2)
>     If Vertically Then theControl.Top = (theForm.ScaleHeight \ 2) - (theControl.Height \ 2)
>
> End Sub
>
> ```

Thanks you so much robin :D
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...