Question

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?

Was it helpful?

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.

OTHER TIPS

It depends what you want should happen when extraform is pushed beyond the bounds of the screen(s).

However, to find out whether or not it's being pushed off, it's quite simple using the System.Windows.Forms.Screens class. Then you can do bounds checking like so:

        foreach (var screen in Screen.AllScreens)
        {
            if(screen.Bounds.Contains(this.Bounds))
            {
                Console.WriteLine("Device "+screen.DeviceName+" contains form!");
            }
        }

Code assumes being in a form. Note that this code only prints that a screen contains the form if the form is completely contained on the screen. But this should be rather simple to fix, depending on your needs.

Perhaps the DesktopLocation property in your Forms can give you a clue about what's happening with what's happening with them

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top