C0D3 MA5T3R Homepage

Object Centraliser

Code to centralise an object on the screen

(See Below Code)

'Dimension the object and set is as a name for a control
Dim object As Control

'Call this function to centralise an object
Public Function central(object As Control)
'
'Set the object's left value as half the entire screen width
object.Left = (Screen.Width / 2) - (object.Width / 2) 'minus half of the object's width
'Set the object's top value as half of the entire screen height
object.Top = (Screen.Height / 2) - (object.Width / 2) 'minus half of the object's height
'
'Finished
'
End Function

'Example of how to use the function:
Private Sub Command1_Click()
'
'Call the function
a = central(Command1) 'With the parameter as the control that is to be centralised - e.g. Command1)
'
'Finished
'
End Sub

(Copy and Paste this code into your Visual Basic project)

*Want to join the C0D3 MA5T3R e-mail list?*

Get all the latest code news and articles, and even submit your own.

You can sign-up for free below (you may opt-out at any time):

Join the mailing list
Enter your name and email address:
Name:
Email:  
Subscribe      Unsubscribe