Skip to content

Instantly share code, notes, and snippets.

@ironfounderson
Created May 24, 2009 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironfounderson/117107 to your computer and use it in GitHub Desktop.
Save ironfounderson/117107 to your computer and use it in GitHub Desktop.
// Sample code to remember size and location of a WinForm.
// Needs a Settings class called FormSettings.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Point location = FormSettings.Default.Location;
if (!StartPositionIsWithinBounds(location))
{
location.X = 10;
location.Y = 10;
}
Location = location;
Size = FormSettings.Default.Size;
}
private static bool StartPositionIsWithinBounds(Point startPosition)
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen.Bounds.Contains(startPosition))
{
return true;
}
}
return false;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
FormSettings.Default.Location = Location;
FormSettings.Default.Size = Size;
FormSettings.Default.Save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment