You should not save the Location
or Size
your form if it is not in a normal state:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (this.WindowState == FormWindowState.Normal) { Settings.Default.WindowLocation = Location; Settings.Default.WindowSize = Size; } Settings.Default.WindowState = WindowState; Settings.Default.Save(); }
Your recovery window procedure does not make full sense. Why keep your location if you center the form? Running the program in minimized mode is probably undesirable, in which case I would use Normal
by default:
private void RestoreWindow() { this.Location = Settings.Default.WindowLocation; this.Size = Settings.Default.WindowSize; // check for size or location off-screen, etc. if ((FormWindowState)Settings.Default.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal; else this.WindowState = Settings.Default.WindowState; }
If you need to restore the last normal position the window was in, you can use the OnResizeEnd
override to save the settings:
protected override void OnResizeEnd(EventArgs e) { if (this.WindowState == FormWindowState.Normal) { Properties.Settings.Default.Location = this.Location; Properties.Settings.Default.Size = this.Size; } base.OnResizeEnd(e); }
Then your closing event:
protected override void OnFormClosing(FormClosingEventArgs e) { Properties.Settings.Default.WindowState = this.WindowState; Properties.Settings.Default.Save(); base.OnFormClosing(e); }
source share