C# Application Save Form Position
C# Application Save Form Position
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.
The settings should be restored when the form loads and saved when the form is closing.
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
Using this
} website you consent to our use of cookies for analysis and advertising. Learn more
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)
this.Location = Properties.Settings.Default.F1Location;
this.Size = Properties.Settings.Default.F1Size;
}
}
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:
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;
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!