Search code examples
c#winforms.net-3.5screen-positioning

How can I tell if my form is displayed beyond the screen?


So I have two forms, mainform and extraform.
extraform is set always moved to the right of mainform when mainform initializes
Sometimes mainform takes up both monitors and extraform is pushed off the screen never to be seen again. I would like to prevent this if possible. How can I do so? It must support dual monitors, that may or may not have distance between them (i.e. screen 1 is 20px to the left of screen 2).

How can I do this?


Solution

  • You can use the Screen class to work out where your window is relative to the desktop. The Screen class has a FromRectangle method, so you can figure out which screen you should be positioning your Form on (by passing your form's Bounds property in).

    Each Screen object has a Bounds property, which you can use to compare to the location and size of your window, and adjust them accordingly.