How to programmatically maximize, minimize and center the form

Posted by: Suprotim Agarwal , on 11/14/2007, in Category WinForms & WinRT
Views: 114955
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 :
C#
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);
}
VB.NET
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 :
C#
 private void btnMax_Click(object sender, EventArgs e)
{
      this.WindowState = FormWindowState.Maximized;
}
VB.NET
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:
 
C#
 
private void btnMin_Click(object sender, EventArgs e)
{
      this.WindowState = FormWindowState.Minimized;
}
 
VB.NET
 
Private Sub btnMin_Click(ByVal sender As Object, ByVal e As EventArgs)
      Me.WindowState = FormWindowState.Minimized
End Sub
 
 
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
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
Thanks  
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 asp.net using code
Comment posted by Ben on Thursday, July 21, 2011 9:17 AM
Or just use:

this.CenterToScreen();

no?
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
Thanks

n

specially thanks ben
and i would like to know ur facebook acount or ur mail id for further assistance...
my id click2jatin@yahoo.com
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

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel