Windows forms position persistence

  • Thread starter Thread starter SagaV9
  • Start date Start date
S

SagaV9

Guest
Hi all,

I am working with VS 2015, C# and Windows 7/10

I have a simple Windows Forms application where I need its position to persist across sessions. The user can size and position the app and then when it is exited and launched again, the size and position of the last session persist. To do this I did some research and successfully implemented it. However, on rare occasion, the app loses the last position and instead positions the app off-screen to coordinates -32,768, -32,768.

I note that this happens sporadically. I have not been able to determine a specific pattern or reason why this happens. The following is the code that I found online and implemented in my own application. I would appreciate it if you can look over it and tell me if there is something incorrect about it. I greatly appreciate your feedback. Thank you for your time. Saga

In the form's closing event I save the form's size and position:


private void frmMDIMain_FormClosing(object sender, FormClosingEventArgs e)
{
//The user is exiting the application.

//Save the size and location of the window in case it changed.
//Save new size and location to Settings

//Save the form size only if the form is not minimized, otherwise,
//set the size to the restored window size.
if (this.WindowState == FormWindowState.Normal)
{
//Form not minimized, save its size.
Settings.Default.WindowSize = this.Size;
}
else
{
//Form is minimized, save the restored size.
//Note: The restored size is the size of the window if it was in its
//normal window state.
Settings.Default.WindowSize = this.RestoreBounds.Size;
}

// Could the following line be saving invalid position coordinates? PENDING

Settings.Default.WindowLocation = this.Location;

//And persist the settings.
Settings.Default.Save();

}


When the application is launched I have the following code in the form's load event. Aside from setting the size and position from the saved settings, I also check to see if the user is holding down the Shift key. If so, then I don't use the saved settings and instead use the default settings for size and position. This is my hack to "fix" the issue when the app is displayed off-screen.

private void frmMDIMain_Load(object sender, EventArgs e)
{

//Create an object that holds the virgin size of this window.
var oInitWinSize = new System.Drawing.Size(0, 0);

//Set initial form size if it is the first run or user pressed Shift key when launching app.
//In either case use default values by getting screen size and using that to mostly fill the
//screen with the MDI window. If it isn't the first run then just use the saved settings.
if (System.Drawing.Size.Equals(oInitWinSize, Settings.Default.WindowSize) ||
Control.ModifierKeys == Keys.Shift)
{
//First run, set window size and location.

//Make MDI form large, but able to fit inside screen, depending on screen size.
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
this.Size = Screen.PrimaryScreen.WorkingArea.Size;

//Save new size and location to Settings
Settings.Default.WindowSize = this.Size;
Settings.Default.WindowLocation = this.Location;

//And persist the settings.
Settings.Default.Save();
}
else
{
//A default size exists, so use that to set the window size and location.
this.Location = Settings.Default.WindowLocation;
this.Size = Settings.Default.WindowSize;
}

}






You can't take the sky from me

Continue reading...
 
Back
Top