How do I minimize, maximize and restore my form programmatically?
You can minimize, maximize and restore the form by setting the WindowState property of the form. The following code example will minimize the form, wait for 2 seconds, restore the form, wait for 2 seconds and then maximize the form. I have written this code on the a button’s Click event.
C# Version
private void button1_Click(object sender, System.EventArgs e)
{
// Minimize the form window
this.WindowState = FormWindowState.Minimized;
Thread.Sleep(2000); // wait for 2 seconds
// Restore the form window
this.WindowState = FormWindowState.Normal;
Thread.Sleep(2000); // wait for 2 seconds
// Maximize the form window
this.WindowState = FormWindowState.Maximized;
}
VB.NET Version
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' Minimize the form window
Me.WindowState = FormWindowState.Minimized
Thread.Sleep(2000) ' wait for 2 seconds
' Restore the form window
Me.WindowState = FormWindowState.Normal
Thread.Sleep(2000) ' wait for 2 seconds
' Maximize the form window
Me.WindowState = FormWindowState.Maximized
End Sub
Note that the above code snippet is using the System.Threading.Thread class’ Sleep method to suspend the form for 2 seconds. Hence, you would be required to include the System.Threading namespace to your form class source code.
C# Version
Using System.Threading;
VB.NET Version
Imports System.Threading
Index