Important alert: (current site time 7/15/2013 7:42:37 AM EDT)
 

VB icon

A Form Center Screen

Email
Submitted on: 3/9/2006 9:22:30 AM
By: nguyen si hoang  
Level: Beginner
User Rating: By 1 Users
Compatibility: C#
Views: 23016
(About the author)
 
     This code provides an useful method to put a form in the center of screen. I have found that Microsoft only provides property "StartPosition" so that when the form is loaded, it is in the center of screen. What if I move it and then want it returns to the center?
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :AForm CenterScreen
//**************************************
This code is created by Nguyen Si Hoang. You may distribute it freely but please refer to author's name
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: AForm CenterScreen
// Description:This code provides an useful method to put a form in the center of screen. I have found that Microsoft only provides property "StartPosition" so that when the form is loaded, it is in the center of screen. What if I move it and then want it returns to the center?
// By: nguyen si hoang
//
// Inputs:Windows Form
//
// Assumes:C# Fundamentals
//
// Side Effects:not at all
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=4567&lngWId=10//for details.//**************************************

/*-------------------------------------------------------------
 Copyright (C) 2006,BlogicSystems. All Rights Reserved.
 Owner:	Nguyen Si Hoang
 Purpose:	Put a form in the center of screen
 Date: March 09 2006
---------------------------------------------------------------*/
private void CenterScreen(Form frm)
{
 // Get the Width and Height of the form
 int frm_width = frm.Width;
 int frm_height = frm.Height;
	
 //Get the Width and Height (resolution) of the screen
 System.Windows.Forms.Screen src = System.Windows.Forms.Screen.PrimaryScreen;
 int src_height = src.Bounds.Height;
 int src_width = src.Bounds.Width;
	
 //put the form in the center
 frm.Left = (src_width - frm_width) / 2;
 frm.Top = (src_height - frm_height) / 2;
}


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 Beginner 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
3/12/2006 1:22:05 PMCrouchie1998

This is basically a VB 6 way of centering the screen changed from 2 lines of code into many more
(If this comment was disrespectful, please report it.)

 
10/26/2006 1:14:19 PMmichael (mkb)

i dont see the point of this code when there is a proprtie to make a project center screen or just 1 line of code

this.StartPosition = FormStartPosition.CenterScreen;


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

 
10/27/2006 9:42:49 PMmichael (mkb)

o i see u want it with in a button and not start still y all the ints

whats rong with
System.Windows.Forms.Screen src = System.Windows.Forms.Screen.PrimaryScreen;
this.Left = (src.Bounds.Width - this.Width) / 2;
this.Top = (src.Bounds.Height - this.Height) / 2;
(If this comment was disrespectful, please report it.)

 
1/20/2007 5:59:56 AMtuan

chang bit down load the nao
(If this comment was disrespectful, please report it.)

 
1/21/2007 4:35:53 AMnguyen si hoang

there's nothing to download. you just copy the code and paste to your code.

michael, you know what i mean. StartPosition only can be used once when the form starts.
(If this comment was disrespectful, please report it.)

 
10/25/2007 2:39:58 AMHemal

Very Nice, Its Nice code..Thanks for posting this.
Kindly give me your mail id so that i can get help from you. my id is hfofaria@yahoo.com
(If this comment was disrespectful, please report it.)

 
1/7/2009 1:57:05 AMchandrakant

this is good for me
(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.