The blog has moved to a new address. The blog is now located at

Sunday, June 11, 2006

Save and restore the size,location and state of a Windows Form in .NET 2.0

The fallowing example saves the properties (location, size and windows state) of a form to app.config
when the form is closed and restores the form state when it is loaded.

Create three properties called Size ,Location and WindowState in project settings dialog as shown in picture:

Modify Form.Load and FormClosing methods (Use DesktopBounds (instead Form.Location and Size)
to place form correctly on a multi screen desktop )

private void Form1_Load( object sender, EventArgs e )


// restore location and size of the form on the desktop

this.DesktopBounds =

new Rectangle(Properties.Settings.Default.Location,


// restore form's window state

this.WindowState = ( FormWindowState )Enum.Parse(




private void Form1_FormClosing( object sender, FormClosingEventArgs e )


Properties.Settings.Default.Location = this.DesktopBounds.Location;

Properties.Settings.Default.Size = this.DesktopBounds.Size;

Properties.Settings.Default.WindowState =

Enum.GetName(typeof(FormWindowState), this.WindowState);

// persist location ,size and window state of the form on the desktop




Technorati : , , ,


Anonymous said...

The Problem here is, if you last loaded the App on the second screen of a dual screen setup, and disable the second screen, the app will load up where you can't see it. I have tried for days to make it load on the visable desktop without success.

Taras said...

You can use Screen class - that represents a display device or multiple display devices on a single system. Check out "AllScreens" and "Primary" properties