How to programmatically maximize, minimize and center the form

Posted by: Suprotim Agarwal , on 11/14/2007, in Category WinForms & WinRT
Views: 247066
Abstract: In this article, we will see how to programmatically center the form using the Screen.PrimaryScreen.Bounds property. We will also use the FormWindowState to Maximize and Minimize the form.
How to programmatically maximize, minimize and center the form
One of the frequently asked questions about Windows Forms 2.0 is to change the position of the forms at runtime or to maximize/minimize the form. In this article, we will see how easy it is to do so.
Center the Form
The Form.StartPosition sets the starting position of the form at runtime. However the resolution and size of the client machine varies. That is one of the reasons that the default value of StartPosition is ‘WindowsDefaultLocation’ which automatically calculates the position based on the resolution.
One way to center the form is set the StartPosition property to ‘CenterScreen’. However, in order to programmatically set the form’s position to center, follow these steps:
Step 1: Drag and drop a button on the form. Rename it to btnCenter
Step 2: On the button click event, add the following code :
private void btnCenter_Click(object sender, EventArgs e)
      int boundWidth = Screen.PrimaryScreen.Bounds.Width;
      int boundHeight = Screen.PrimaryScreen.Bounds.Height;
      int x = boundWidth - this.Width;
      int y = boundHeight - this.Height;
      this.Location = new Point(x / 2, y / 2);
Private Sub btnCenter_Click(ByVal sender As Object, ByVal e As EventArgs)
       Dim boundWidth As Integer = Screen.PrimaryScreen.Bounds.Width
       Dim boundHeight As Integer = Screen.PrimaryScreen.Bounds.Height
       Dim x As Integer = boundWidth - Me.Width
       Dim y As Integer = boundHeight - Me.Height
       Me.Location = New Point(x / 2, y / 2)
End Sub
The Screen.PrimaryScreen.Bounds gets the bounds of the display screen. The width and height of the display is subtracted with the width and height of the form. Once we get this calculated value, set the location by passing the value to Point().
Maximize the Form
Step 1: Drag and drop a button on the form. Rename it to btnMax
Step 2:  On the click event, write the following code :
 private void btnMax_Click(object sender, EventArgs e)
      this.WindowState = FormWindowState.Maximized;
Private Sub btnMax_Click(ByVal sender As Object, ByVal e As EventArgs)
Me.WindowState = FormWindowState.Maximized
End Sub
Minimize the Form
Step 1: Drag and drop a button on the form. Rename it to btnMin
Step 2: On the click event, write the following code:
private void btnMin_Click(object sender, EventArgs e)
      this.WindowState = FormWindowState.Minimized;
Private Sub btnMin_Click(ByVal sender As Object, ByVal e As EventArgs)
      Me.WindowState = FormWindowState.Minimized
End Sub

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by siddhartha on Thursday, February 12, 2009 4:04 AM
I love this help by u.........thnxs
Comment posted by Arasu on Wednesday, March 25, 2009 8:48 AM
Comment posted by mohan raju on Sunday, April 12, 2009 6:35 AM
hello sir thanks for giving this type answers....
Comment posted by ashish kumar on Wednesday, April 22, 2009 4:36 AM
cool: very easy and helpful
Comment posted by KK on Friday, November 6, 2009 4:48 AM
How do you Maximize the form in a Dual Monitor?
Comment posted by KK on Friday, November 6, 2009 5:10 AM
How do you Maximize the form in a Dual Monitor?
Comment posted by April on Tuesday, November 10, 2009 12:30 AM
thank you very much on this info....
Comment posted by Maulik Makadiya on Monday, February 8, 2010 4:45 AM
How to minimize the internet explorar in using code
Comment posted by Ben on Thursday, July 21, 2011 9:17 AM
Or just use:


Comment posted by Janith Jayaweera on Sunday, August 21, 2011 12:04 AM
When the form is maximized how to re-size the objects inside the form accordingly in C#?
Comment posted by jatin on Sunday, September 4, 2011 11:42 AM


specially thanks ben
and i would like to know ur facebook acount or ur mail id for further assistance...
my id
Comment posted by Unni on Monday, August 20, 2012 6:04 AM
Yes.Ideed it was very good.
Comment posted by Niranjan Maharana on Monday, September 24, 2012 4:22 AM
I have got real solution for my project.
Comment posted by sagar on Friday, December 14, 2012 1:13 AM
thanx a lot....
Comment posted by prasanth on Wednesday, April 10, 2013 1:20 PM
Really helpful..thanks a lot..
Comment posted by Vamsinath Gadwal on Sunday, April 14, 2013 12:11 AM
thank you
Comment posted by suvi on Thursday, June 6, 2013 4:28 AM
individual buttons it works but for maximize and minimize in single button like our windows form .... its not we need to write condition
Comment posted by Ramesh on Wednesday, October 15, 2014 2:57 AM
Thank u so much sir.