BenEskew.com Just another web developer's personal weblog.

Register.com Hosting 468x60
11Sep/090

Visual Basic Code Snippets #3

In this next update I’ll continue to re-post my Visual Basic code snippets (which are older than five years by the way) that pertain to Forms.

Center a Form on the Screen

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Sub Form_Center(Frm As Form)
'Usually used in form_load
Frm.Left = Screen.Width / 2 - Frm.Width / 2
Frm.Top = Screen.Height / 2 - Frm.Height / 2
End Sub

Add a form named Form1 and add the following code wherever you want the code to execute...

Call Form_Center(Form1)

Center Form at a Given Point on the Screen

Compatibility: Win. 98-XP

Add the following code into a module within your project...

Public Sub Form_CenterAt(Frm As Form, X As Integer, Y As Integer)
'X: X coordinate to center form at
'Y: Y coordinate to center form at
'Example...
'Form_CenterAt me, screen.width/2, screen.height/2
Frm.Left = X - Frm.Width / 2
Frm.Top = Y - Frm.Height / 2
End Sub

Add a form named Form1 and add the following code wherever you want the code to execute...

Call Form_CenterAt(Form1, screen.width/2, screen.height/2)

Visual Basic Code Snippets #4