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

[center a form]

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 Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in forms and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *