0% found this document useful (0 votes)
79 views4 pages

C# Application Save Form Position

This document provides steps to save and restore the position and size of a C# Windows Forms application window. It describes adding settings to store the window state, location and size, and loading/saving these in the form load and closing events.

Uploaded by

Muhammad Nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views4 pages

C# Application Save Form Position

This document provides steps to save and restore the position and size of a C# Windows Forms application window. It describes adding settings to store the window state, location and size, and loading/saving these in the form load and closing events.

Uploaded by

Muhammad Nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

 Knowledge Base

   Cookies & Privacy

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".

Using this website you consent to our use of cookies for analysis and advertising. Learn more

Got it!
 Knowledge Base    Cookies & Privacy

Taxiappen Mivai
Finn beste tilbud. Bestill direkte og betal i appen.

Mivai OPEN

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)


{

Using this
} website you consent to our use of cookies for analysis and advertising. Learn more

private void Form1_Closing(object sender, FormClosingEventArgs e)


Got it!
{
}  Knowledge Base    Cookies & Privacy

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!
Using this website you consent to our use of cookies for analysis
Properties.Settings.Default.F1Location and advertising. Learn more
= this.RestoreBounds.Location;
Properties.Settings.Default.F1Size = this.RestoreBounds.Size;
}
Got it!
// don't forget to save the settings
Properties.Settings.Default.Save();
}  Knowledge Base
   Cookies & Privacy

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;
}
}

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

©1997-2020 Arclab®. All other trademarks and brand names are the property of their respective owners.   Cookies & Privacy

Using this website you consent to our use of cookies for analysis and advertising. Learn more

Got it!

You might also like