I support a desktop application that is written in C# and runs on windows forms. The application has a number of pop up forms (usually launched by clicking on a button), throughout the application. I have a request from the users to allow for them to choose which monitor they want the application to launch from (by default) and to save a store that information. With the Covid-19 pandemic, my company is in a situation where a lot of employees are working from home. A typical setup is a user with a laptop, probably plugged into a universal docking station, and there is a 2nd monitor attached. The users want the application to launch on the 2nd monitor versus the primary monitor (which is the laptop's monitor).
I seem to be running into issues when I set the default monitor to the 2nd (non-primary) monitor. Most of the forms will launch from the secondary monitor, however, not ALL of the forms will launch from the secondary monitor. It's a mystery to me at this point, hence this post. For example, I have the exact same code for two different forms that launch off a given form. One will launch on the current monitor (the second monitor). The other form will insist on launching on the primary monitor. Here is the code that I've tried.
//above this point, I'm passing in parameters
var screen = Screen.FromPoint(Cursor.Position);
myform.StartPosition = FormStartPosition.Manual;
myform.Left = screen.Bounds.Left + screen.Bounds.Width / 2 - myform.Width / 2;
myform.Top = screen.Bounds.Top + screen.Bounds.Height / 2 - myform.Height / 2;
Stepping through the code, here is what I see when I hover over fields in my code:
screen- {Bounds = {{X=1600,Y=0,Width=1440,Height=900}} WorkingArea = {{X=0,Y=0,Width=0,Height=0}} Primary = false DeviceName = "\\\\.\\DISPLAY7"}
That is definitely screen #2.
myform.Left = 1988
myform.Top = 237
Again, should have launched on screen #2.
I've also tried (instead of those 4 lines of code):
StartPosition = FormStartPosition.Manual;
OpusForms.fProductDetailForm.Location = Screen.AllScreens[getIndexofSelectedMonitor()].WorkingArea.Location;
getIndexofSelectedMonitor() is a function that I built to retrieve the index of the saved monitor (1 in this case).
I also tried hard coding the solution:
myform.StartPosition = FormStartPosition.Manual;
myform.Location = Screen.AllScreens[1].WorkingArea.Location;
And then below each of the 3 segments, I launch the new form with:
myform.ShowDialog();
In all cases, the form insists on launching on the primary monitor, and I'm not sure why. Any help is appreciated!