|
Validating Form Settings
Published: 10/21/2008 |
|
I recently was working on a laptop at work and for one day had it hooked up to a monitor and was using both screens - extending the desktop from one to the other. The next day I went back to working just on the laptop screen and when I launched the program I had been working on couldn't figure out why I couldn't see my main form - even though it was showing in the taskbar. Turns out that the code I wrote to remember the last position of the form on startup didn't take into account the fact that the second screen was missing, and was positioning the form offscreen - where I had left it.
So I wrote this handy routine to validate the form settings on startup:
VB:Dim boolFitsScreen As Boolean = False For Each clsScreen As Screen In Screen.AllScreens If (this.Location.X < (clsScreen.WorkingArea.Width - this.Size.Width) And this.Location.X > clsScreen.WorkingArea.X) Then If (this.Location.Y < (clsScreen.WorkingArea.Height - this.Size.Height) And this.Location.Y > clsScreen.WorkingArea.Y) Then boolFitsScreen = True End If End If Next If (Not boolFitsScreen) Then Me.Location = New Point(25, 25)
bool boolFitsScreen = false;
foreach (Screen clsScreen in Screen.AllScreens)
{
if (this.Location.X < (clsScreen.WorkingArea.Width - this.Size.Width) && this.Location.X > clsScreen.WorkingArea.X)
if (this.Location.Y < (clsScreen.WorkingArea.Height - this.Size.Height) && this.Location.Y > clsScreen.WorkingArea.Y)
boolFitsScreen = true;
}
if (!boolFitsScreen) this.Location = new Point(25, 25);
Questions or Comments? .
VB to C# and C# to VB translation provided by Instant C# and Instant VB.