Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi All,
 
I have been asked to position a Form in the center of the screen. I have in the past used the Location for this (back in the day VB6 used to have a picture of where the form would appear, wow something I miss about VB6!) but I think this program is going to be used on many things (Net books, (shudder!) Tablets). It just in looking around I have found Form and Control Position and Size Utility[^] which seems a little too indepth for what I am after.
 
Sorry
Glenn,
Ah I have found Windows Forms - Creating and Loading the Main Window Form and Child Control States Using C#[^] will this give what I am looking for!
Posted 17-Jan-13 3:52am
Edited 17-Jan-13 3:56am
v2
Comments
BillWoodruff at 17-Jan-13 11:05am
   
Setting the FormStartPosition property seemed so obvious to me, that I thought your question was going to be: how to keep the Form in the center of the Screen when the Form is re-sized at run-time :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this:
form.Location = new Point((Screen.PrimaryScreen.Bounds.Size.Width / 2) - (form.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (form.Size.Height / 2)); 
  Permalink  
Comments
glennPattonWork at 17-Jan-13 10:10am
   
I can see to get this to work...
I have had to mod the line Form1.Location =
new Point((Screen.PrimaryScreen.Bounds.Size.Width / 2) - (Form1.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (Form1.Size.Height / 2));
so all reference are to Form1 rather than form I have tried it after InitalizeComponents in the public Form1, in the Form1_Load() it seems to error on
the Form1 refernces pasted message Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Form.Location.get'
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi.
 
If you are working with Windows Forms...
 
You can set StartPosition property directly from designer or through code, just before showing it.
 
form.StartPosition = FormStartPosition.CenterScreen;
 
Possible values are:
Manual
CenterScreen
WindowsDefaultLocation
WindowsDefaultBounds
CenterParent
 
Form.StartPosition Property
FormStartPosition Enumeration
 
Daniele.
  Permalink  
Comments
glennPattonWork at 17-Jan-13 10:19am
   
Ahh, thanks for that!
Glenn
Daniele Rota Nodari at 17-Jan-13 14:12pm
   
Someone did not like this solution.
Can this one kindly explain what's wrong?
glennPattonWork at 18-Jan-13 7:58am
   
Not me sir!
Daniele Rota Nodari at 18-Jan-13 8:17am
   
I know :) and I hope it really solved your issue.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Or you could use the tool in the Form designer, called Start Position, I am sorry for wasting time. Just got home looked at Visual Studio and swore!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Schatak 394
1 OriginalGriff 340
2 _Amy 115
3 Abhijit Ghosh (Subho) 115
4 Andreas Gieriet 100
0 OriginalGriff 7,107
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,504
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100