Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display the form on the extended monitor. Below is my code after clicking a button:
C#
primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0);
extendedDisplay = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryDisplay)) ?? primaryDisplay;
this.StartPosition = FormStartPosition.Manual;
this.Location = extendedDisplay.Bounds.Location;
this.Show();

However, the form is still displayed on the primary one. What is wrong in my code? Thanks.

What I have tried:

Display Form on Extended Screen not successful
Posted
Updated 26-Sep-17 3:51am

1 solution

The order in which the screens are returned is not defined anywhere I can see: you need to check the Screen.Primary Property (System.Windows.Forms)[^] rather than rely on the first in the colection:
C#
primaryDisplay = Screen.AllScreens.FirstOrDefault(s => s.Primary);
extendedDisplay = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryDisplay;
StartPosition = FormStartPosition.Manual;
Location = extendedDisplay.Bounds.Location;
Show();
 
Share this answer
 
v2
Comments
s yu 26-Sep-17 11:02am    
Solved per your solution. Thanks.
OriginalGriff 26-Sep-17 12:19pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900