[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Center a form on the screen at run time in C#

[Center a form on the screen at run time in C#]

You can center a form at design time by setting its StartPosition property to CenterScreen. Note that you cannot do this in the form's Load event handler because the form's position is already set when that event handler executes. You can do it in the form's constructor, but it's usually considered bad style to modify the constructor.

To center the form at design time, either in the Load event handler or in some other part of the program, you need another approach to center a form. You could use the size of the form, the size of the screen, and the location and size of the task bar to figure out where to move the form to center it. Fortunately there's a much easier way to center a form. Simply invoke the form's CenterToScreen method as in the following code.

this.CenterToScreen();

Sometimes things are a lot easier than you might think, at least if you know what method to use.

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.