Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 17-Jan-13 3:56am
v2
Comments
BillWoodruff 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 :)

Try this:
C#
form.Location = new Point((Screen.PrimaryScreen.Bounds.Size.Width / 2) - (form.Size.Width / 2), (Screen.PrimaryScreen.Bounds.Size.Height / 2) - (form.Size.Height / 2)); 
 
Share this answer
 
Comments
glennPattonWork3 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'
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.
 
Share this answer
 
Comments
glennPattonWork3 17-Jan-13 10:19am    
Ahh, thanks for that!
Glenn
Daniele Rota Nodari 17-Jan-13 14:12pm    
Someone did not like this solution.
Can this one kindly explain what's wrong?
glennPattonWork3 18-Jan-13 7:58am    
Not me sir!
Daniele Rota Nodari 18-Jan-13 8:17am    
I know :) and I hope it really solved your issue.
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900