Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET
Tip/Trick

Show Forms Current Monitor on Dual (or more) Monitors

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
5 Jul 2015CPOL2 min read 30.1K   8   5
How to show your forms in the current active display on dual (or more) monitors

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:

VB.NET
// 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.

VB.NET
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

  • Version 1.0

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBetter way Pin
Eric P Schneider31-Jan-19 5:03
Eric P Schneider31-Jan-19 5:03 
Dim ds As New FormDiagramSettings
ds.StartPosition = FormStartPosition.CenterParent
ds.ShowDialog(Me)

Schneider

Generalcould you upload your source code? Pin
Southmountain5-Jul-15 9:07
Southmountain5-Jul-15 9:07 
GeneralRe: could you upload your source code? Pin
Tantum7-Jul-15 2:01
Tantum7-Jul-15 2:01 
QuestionAssumptions Pin
dandy725-Jul-15 6:45
dandy725-Jul-15 6:45 
AnswerRe: Assumptions Pin
gaohe824-Aug-15 23:27
gaohe824-Aug-15 23:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.