Save / Restore Windows App Form Location
Save & Restore the screen size and location of a windows form
The following illustrates how to save & restore the location of a windows application. Net result is that when an application is started, its form is in same location and the same size that it was when it was last closed.
Create user properties
Create 2 user properties in the Windows application project.Property Name | Type | Scope | Value |
---|---|---|---|
WinLocation | System.Drawing.Point | User | 0,0 |
WinSize | System.Drawing.Size | User | 0,0 |
Create methods to save and restore window location and size
Save Methodprivate void SaveSizeAndPosition() { // save the form location & size to the property values Properties.Settings.Default.WinLocation = this.Location; Properties.Settings.Default.WinSize = this.Size; // save the property settings Properties.Settings.Default.Save(); }
Restore Method
private void RestorePreviousLocation() { if (Properties.Settings.Default.WinSize.Width <= 0 || Properties.Settings.Default.WinSize.Height <= 0) { // width / height not set, don't attempt to update the size/location of the window // do the upgrade of the property settings Properties.Settings.Default.Upgrade(); } else { // set the size & location from the property settings this.Location = Properties.Settings.Default.WinLocation; this.Size = Properties.Settings.Default.WinSize; } }