Introduction
Sometimes, in Dual or Triple Screens, your application started in Primary monitor, but users change the location for the other one (2nd or 3rd one). In these cases, some subsequent Custom Dialogs and Forms can appear wrongly in the Primary Screen if their location is set to Screen.CenterScreen
.
These few lines in the Form.Load
event can correct this, ensuring that your form or custom-dialog will appear always in the same monitor your main window resides.
Background
This code is made in Visual Studio VB.NET 2013, so, if you program in that platform, you can easily put the code into your own application.
Using the Code
Open your application in Visual Studio. Double click your additional forms (not the FORM1 or MAIN one) and put these lines into the Form.Load
event.
Note that you need a control in the main form to discover where that form is located in the multi-monitor system. In this sample, I will point a table control called "TableMain
" in the main-form:
// Dim Current_Screen As Screen = Screen.FromControl(Form1.TableMain)
The above line can indicate to us if our application is, at the present moment, running on the Primary Screen or not.
If so, nothing to do - the automatic Windows.Location
property can function normally. But, if the application would not be in the Primary Screen, it may be located in the second (or third) monitor.
Windows refers to these systems as a huge-width system. For instance, in a dual-monitor system (both running in 1920x1080), Windows understands that the second monitor has a left bound of 1921 (one pixel more than the right bound of the first (or Primary) monitor. So, we need only to discover which monitor our main form resides and manually change this form location according to these bounds.
In the sample below, I will center manually my Custom Dialog in the same monitor my main form is.
If Current_Screen.Primary = False Then
Dim HCenter = Current_Screen.Bounds.Left + _
(((Current_Screen.Bounds.Right - Current_Screen.Bounds.Left) / 2) - ((Me.Width) / 2))
Dim VCenter = (Current_Screen.Bounds.Bottom / 2) - ((Me.Height) / 2)
Me.StartPosition = FormStartPosition.Manual
Me.Location = New Point(HCenter, VCenter)
Else
Me.StartPosition = FormStartPosition.CenterScreen
End If
I believe your application can be built a little bit more professional-like if all of its forms appear in the same monitor the main form resides. Important thing is this routine automatically adapts itself even if user changes the form from one monitor to another. Your subsequent forms and dialogs will always appear in the current monitor.
I hope it can be useful to you.
History