Created
May 24, 2009 13:57
-
-
Save ironfounderson/117107 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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