evilbunnie Posted February 20, 2011 Author Share Posted February 20, 2011 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 WabbitSub 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 More sharing options...
Robin Posted February 20, 2011 Share Posted February 20, 2011 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 More sharing options...
evilbunnie Posted February 21, 2011 Author Share Posted February 21, 2011 @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 More sharing options...
Robin Posted February 21, 2011 Share Posted February 21, 2011 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 More sharing options...
evilbunnie Posted February 22, 2011 Author Share Posted February 22, 2011 @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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now