How do I determine which monitor my .NET Windows Forms program is running on?

Stephen Fletcher picture Stephen Fletcher · Jul 13, 2009 · Viewed 41.1k times · Source

I have a C# Windows application that I want to ensure will show up on a second monitor if the user moves it to one. I need to save the main form's size, location and window state - which I've already handled - but I also need to know which screen it was on when the user closed the application.

I'm using the Screen class to determine the size of the current screen but I can't find anything on how to determine which screen the application was running on.

Edit: Thanks for the responses, everyone! I wanted to determine which monitor the window was on so I could do proper bounds checking in case the user accidentally put the window outside the viewing area or changed the screen size such that the form wouldn't be completely visible anymore.

Answer

Stan R. picture Stan R. · Jul 13, 2009

You can get an array of Screens that you have using this code.

Screen[] screens = Screen.AllScreens;

You can also figure out which screen you are on, by running this code (this is the windows form you are on)

Screen screen = Screen.FromControl(this); //this is the Form class

in short check out the Screen class and static helper methods, they might help you.

MSDN Link, doesn't have much..I suggest messing around in the code by yourself.