Important alert: (current site time 7/16/2013 1:38:05 AM EDT)
 

VB icon

!Center Your Form!

Email
Submitted on: 8/1/1999
By: Stephen Glauser  
Level: Not Given
User Rating: By 8 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 33533
 
     This will Center your form on your screen. It will also higher it a tiny bit, so it looks centered above the TaskBar.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: !Center Your Form!
' Description:This will Center your form on your screen. It will also higher it a tiny bit, so it looks centered above the TaskBar.
' By: Stephen Glauser
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2736&lngWId=1'for details.'**************************************

Sub FormCenter (Frm As Form)
Rem Written by Stephen Glauser
Rem Last Update: August 1, 1999
Rem Centers your form on the Screen
Rem FormCenter Me
Frm.Top = (Screen.Height * .85) / 2 - Frm.Height / 2
Frm.Left = Screen.Width / 2 - Frm.Width / 2
End Sub


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/2/1999 4:45:00 AMViper-One

Okay code,But!! what if the Taskbar is being invisble sometimes.
(If this comment was disrespectful, please report it.)

 
8/2/1999 10:53:00 AMAaronC

With VB5 (And I assume 6), there is a form option that allows you to set where the window will be on startup. If you just set this to Center of Screen, it should center it.
(If this comment was disrespectful, please report it.)

 
8/2/1999 1:23:00 PMSteve L

this is all you need for VB 3.0 +
to center your form to the center of the screen same as above.
code:

Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
(If this comment was disrespectful, please report it.)

 
1/10/2000 7:26:41 PMTom McGovern

I like this. I'm a VB newbie and I've been trying to find an easy way to put the form where I want it for a couple of projects where the form will be always on top but when exactly centered it gets in the way of other programs.
(If this comment was disrespectful, please report it.)

 
8/10/2000 1:43:19 AMAlex

Just thought you'd like to know: MoSHPuNTeR-TNS-2k took this code and called it his own... it even has yiour name on it!!!
(If this comment was disrespectful, please report it.)

 
8/10/2000 1:43:27 AMAlex

Just thought you'd like to know: MoSHPuNTeR-TNS-2k took this code and called it his own... it even has your name on it!!!
(If this comment was disrespectful, please report it.)

 
3/25/2002 9:43:43 AM§øûrÇÊ ÇödÊR

actually there arent many other ways to do this alex..it is $tupid that he stole the code but...
(If this comment was disrespectful, please report it.)

 
6/11/2002 6:33:33 PMMonolith

This code doesn't seem to work for me. Anything I try to center is offset towards the left.
(If this comment was disrespectful, please report it.)

 
6/23/2002 10:28:50 PMIanHafkenschiel

To elaborate on Aaron's idea: Set the 'StartUpPosition' in your form's properties to '2 - CenterScreen'. Unfortunately this only can be done in design time.
(If this comment was disrespectful, please report it.)

 
12/21/2002 1:57:26 PMBAS_ARM

Screen object does not work always properly in VB6 compiled applications (1 in 7 or 8 one of the properties it takes the value of the other).
So I expected to see other things.
(If this comment was disrespectful, please report it.)

 
12/21/2002 10:33:45 PM

To get this code to work for me i changed my Form name to frm and erase everything except the
Frm.Top = (Screen.Height * .85) / 2 - Frm.Height / 2
Frm.Left = Screen.Width / 2 - Frm.Width / 2
End Sub

and at the top i put Private Sub Form_Load() just thought you'd like to know...
(If this comment was disrespectful, please report it.)

 
12/22/2002 11:08:57 AM

This is post-beginner stuff. I'm disappointed to find that it got code of the day.
(If this comment was disrespectful, please report it.)

 
12/23/2002 9:15:14 AM

What happens if your start bar hidden? What happens if your start bar is 2 columns high (like mine)? This code needs to be researched more to find out the exact size of the start bar, then it'll be good.
(If this comment was disrespectful, please report it.)

 
3/5/2003 9:15:45 PM

It would be easier if you just went into the form properties window and changed the StartUpPosition to Center On Screen, simple as that
(If this comment was disrespectful, please report it.)

 
3/31/2003 1:39:07 AM

Really a nice thing.. for making ur GUI look more professional that give ur software an extra edge toward others
(If this comment was disrespectful, please report it.)

 
6/5/2003 1:58:57 AMVec Fusion

You can't copyright code chief. Microsfot created vb and if you think that because you figured out some new fab code and it is copyrighted, think again, if you post it here it is public domain. The only way you can copyright it is if you only release it in an exe format etc
(If this comment was disrespectful, please report it.)

 
8/22/2003 2:51:20 PM

gercekten guzel saol
(If this comment was disrespectful, please report it.)

 
8/22/2003 2:51:46 PM

saol güzel bir kod
(If this comment was disrespectful, please report it.)

 
3/26/2004 2:17:20 AM

I'm a beginner and I just confuse with so many such that comment...
(If this comment was disrespectful, please report it.)

 
3/10/2005 1:19:04 AM

I know
(If this comment was disrespectful, please report it.)

 
5/12/2006 8:36:00 AMSantu Ghosh

good
(If this comment was disrespectful, please report it.)

 
10/29/2006 2:51:20 PMKenny Roberts

OK, well to explain about this.. Yes, for startup you can use the nice nifty VB option, but what if the user changes their resolution?? Or what if they move the form, and you want to call an update to center it again. Or perhaps, your making a theme manager, like I am.. And you need it to refresh to the center, but want to allow the users move it freely, hence you cant use the lock window feature. I think I have made my point, if you want to have a debate.. Then I suggest you either get a life (nicest way I can put it), or email me at bugs181@hotmail.com
(If this comment was disrespectful, please report it.)

 
10/29/2006 2:54:39 PMKenny Roberts

Oh btw, Stephen, you did a good job. Sorry, you cant copyright this code. But anyways, thanks for sharing.. 5 Globes for you. And more constructive criticism: People you need to get a life, quit badgering people on PSC.. PSC made to share ideas and code. Its as simple as that, some arent as good as others. But at least they try, more than what I can say for half of the people that badger people's work.
(If this comment was disrespectful, please report it.)

 
12/31/2006 5:12:07 AMAshwin

thanx steve l for that 1 line code

(If this comment was disrespectful, please report it.)

 
1/1/2007 3:16:10 AMShucayb

Thanx lot, the code is working properly and I pasted in the module as public procedure then I passed the form object.
(If this comment was disrespectful, please report it.)

 
2/23/2007 3:09:32 AMAndualem

I can't download codes, Can you tell what should I do
(If this comment was disrespectful, please report it.)

 
1/31/2008 7:23:24 AMChris Glass

Me.StartUpPosition = 2

lol.
(If this comment was disrespectful, please report it.)

 
1/18/2010 3:59:35 PMsivasweety

'additional to be added
Private Sub Form_Load()
Call FormCenter(frm) ' AS Form)
End Sub
'form name to be frm
' now it will work.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.