Not logged in - Login
< back

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 NameTypeScopeValue
WinLocationSystem.Drawing.PointUser0,0
WinSizeSystem.Drawing.SizeUser0,0

Create methods to save and restore window location and size

Save Method


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