C# Windows Forms Application
Save and restore position and size of a windows form
Every C# Windows Forms Application should save it's position, size and state for a positive user experience. The following tutorial shows how to save the windows position in the settings when closing the program and how to restore the window when the program is started again.
We need the state, location and size of the form:
- this.WindowState ... maximized, normal, etc.
- this.Location ... window position x/y
- this.Size ... width and height
The settings should be restored when the form loads and saved when the form is closing.
Step 1: Add Variables to Application Settings
Go to "Properties" > "Settings.settings" and add e.g. the following settings:
- Name: F1State | Type: System.Windows.Forms.FormWindowState | Value: Normal
- Name: F1Location | Type: System.Drawing.Point | Value: 0;0
- Name: F1Size | Type: System.Drawing.Size | Value: 0;0
Step 2: Add the Form Load Function
Open the form, go to "Properties" > "Events" > "Load", in e.g. Form1_Load and press "Enter".
Step 3: Add the Form Closing Function
Go to "Properties" > "Events" > "FormClosing", type in e.g. Form1_Closing and press "Enter".
Step 4: Locate the Load and Closing Functions
Visual Studio has added the load and closing function to your "form1.cs".
e.g.
private void Form1_Load(object
sender, EventArgs e)
{
}
private void Form1_Closing(object
sender, FormClosingEventArgs e)
{
}
Step 5: Add Code to Form1_Load
Set the state, position and size when the form loads:
private void Form1_Load(object sender,
EventArgs e)
{
this.WindowState = Properties.Settings.Default.F1State;
this.Location = Properties.Settings.Default.F1Location;
this.Size = Properties.Settings.Default.F1Size;
}
The values from the application settings are ZERO the first time, so we need to adjust the code. (We don't want a form with height=0 or width=0)
private void Form1_Load(object sender,
EventArgs e)
{
if (Properties.Settings.Default.F1Size.Width==0
|| Properties.Settings.Default.F1Size.Height==0)
{
// first start
// optional: add
default values
}
else
{
this.WindowState = Properties.Settings.Default.F1State;
// we don't want a minimized
window at startup
if (this.WindowState
== FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
this.Location = Properties.Settings.Default.F1Location;
this.Size = Properties.Settings.Default.F1Size;
}
}
Step 6: Add Code to Form1_Closing
Save state, position and size when the form is closing.
private void Form1_Closing(object
sender, FormClosingEventArgs e)
{
Properties.Settings.Default.F1State
= this.WindowState;
if (this.WindowState==FormWindowState.Normal)
{
// save location and size if
the state is normal
Properties.Settings.Default.F1Location
= this.Location;
Properties.Settings.Default.F1Size
= this.Size;
}
else
{
// save the RestoreBounds if
the form is minimized or maximized!
Properties.Settings.Default.F1Location = this.RestoreBounds.Location;
Properties.Settings.Default.F1Size = this.RestoreBounds.Size;
}
// don't forget to save the settings
Properties.Settings.Default.Save();
}
Step 7: Define a Minimum Form Size (Optional)
Define a minimum form size in the "Properties" > "Properties" > "Minimum Size".
Step 8: Keep the Settings after Upgrading the Assembly Version (Optional)
User settings are usually lost when upgrading to a new version of a C# desktop
application.
The easiest way to fix this is to call: Properties.Settings.Default.Upgrade();
The upgrade function searches for previous versions of your application
in the (User) App Data directory and copies the user settings to the new version.
Upgrade should only be called the first time after an upgrade of the version
number.
We can e.g. use the "F1Size.Width" for this purpose ... if the
width is 0 (the default value in the user settings table) then the application
was started the first time or the first time after an upgrade of the version
number.
It's important to add the code before accessing the user
settings, e.g. in the Form1_Load function:
private void Form1_Load(object sender,
EventArgs e)
{
// Upgrade?
if (Properties.Settings.Default.F1Size.Width==0) Properties.Settings.Default.Upgrade();
if (Properties.Settings.Default.F1Size.Width==0 || Properties.Settings.Default.F1Size.Height==0)
{
// first start
// optional: add default values
}
else
{
this.WindowState
= Properties.Settings.Default.F1State;
// we don't want a minimized
window at startup
if (this.WindowState
== FormWindowState.Minimized) this.WindowState = FormWindowState.Normal;
this.Location = Properties.Settings.Default.F1Location;
this.Size = Properties.Settings.Default.F1Size;
}
}