VB and VBA Users Source Code: Centering a form within a form (useful for MDI projects)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Centering a form within a form (useful for MDI projects)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, May 29, 2002
Hits:
774
Category:
Windows Forms/GUI/Controls/Graphics
Article:
The following code centers a dialog/form over a parent form or on the desktop. This is useful in MDI projects as you can't set the StartUpPosition of child forms. 'Purpose : Centers a form within a form or on the desktop 'Inputs : oFrm The form to center ' [oFrmOwner] Optional, the form to center the form on. If ommitted will ' center the form across the desktop 'Outputs : N/A 'Author : Andrew Baker 'Date : 23/07/2000 18:52 'Notes : 'Revisions : Sub FormCenter(oFrm As Form, Optional oFrmOwner As Object) If (oFrmOwner Is Nothing) = False Then If oFrm.MDIChild = True Then 'Center form over MDI parent oFrm.Left = (oFrmOwner.ScaleWidth / 2) - (oFrm.width / 2) oFrm.Top = (oFrmOwner.ScaleHeight / 2) - (oFrm.Height / 2) Else 'Center form over parent oFrm.Left = oFrmOwner.Left + (oFrmOwner.width / 2) - (oFrm.width / 2) oFrm.Top = oFrmOwner.Top + (oFrmOwner.Height / 2) - (oFrm.Height / 2) End If Else 'Center form over whole screen oFrm.Left = (Screen.width / 2) - (oFrm.width / 2) oFrm.Top = (Screen.Height / 2) - (oFrm.Height / 2) End If End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder