Click here to Skip to main content
11,930,069 members (49,380 online)
Rate this:
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.

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 4:52am
Edited 17-Jan-13 4:56am
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 :)
Rate this: bad
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)); 
glennPattonWork 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
Please Sign up or sign in to vote.

Solution 2


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:

Form.StartPosition Property
FormStartPosition Enumeration

glennPattonWork 17-Jan-13 10:19am
Ahh, thanks for that!
Daniele Rota Nodari 17-Jan-13 14:12pm
Someone did not like this solution.
Can this one kindly explain what's wrong?
glennPattonWork 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.
Rate this: bad
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!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
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