I am developing an application that remembers the user's preferences as to where the form was last located on the screen. In some instances the user will have it on a secondary screen, and then fire the app up later without the second screen (sometimes having the form appear off screen). Other times the user will change their resolution resulting in a similar effect.
I was hoping to do this checking in the Form_Shown event handler. Basically I want to determine whether the form is completely off screen so I can re-position it.
Any advice?
Click on the tab for the second form (the subForm) in your design and double click on the button control to display the Click event procedure. Press F5 to build and run the application. Click on the button in the main form to display the sub form. Now, when you press the button in the sub form, the form will be hidden.
Introduction. Windows Forms is a UI framework for building Windows desktop apps. It provides one of the most productive ways to create desktop apps based on the visual designer provided in Visual Studio. Functionality such as drag-and-drop placement of visual controls makes it easy to build desktop apps.
Set the form's StartPosition property to CenterScreen .
Check with this function if Form is fully on screen:
public bool IsOnScreen( Form form ) { Screen[] screens = Screen.AllScreens; foreach( Screen screen in screens ) { Rectangle formRectangle = new Rectangle( form.Left, form.Top, form.Width, form.Height ); if( screen.WorkingArea.Contains( formRectangle ) ) { return true; } } return false; }
Checking only top left point if it's on screen:
public bool IsOnScreen( Form form ) { Screen[] screens = Screen.AllScreens; foreach( Screen screen in screens ) { Point formTopLeft = new Point( form.Left, form.Top ); if( screen.WorkingArea.Contains( formTopLeft ) ) { return true; } } return false; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With