How to programmatically maximize, minimize and center the form
Posted by: Suprotim Agarwal ,
on 11/14/2007,
in
Category WinForms & WinRT
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
This article has been editorially reviewed by Suprotim Agarwal.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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 Sixteen consecutive years. 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