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:

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:

Disclaimer: The information on this page is provided "as is" without warranty of any kind.
Further, Arclab Software does not warrant, guarantee, or make any representations
regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise.
See: License Agreement