Displaying forms automatically on different monitors

DouglasBell

Active member
Joined
Aug 16, 2011
Messages
38
Programming Experience
Beginner
Hi all

I have created an application that uses two forms one is displayed on a touchscreen and is a basic input form, the other is displayed on a large display monitor and is basically an information form, both displays are connected to the same pc. The program works great I have just one annoyance is if the program is restarted it displays both forms on the touchscreen and I have to drag the information form onto the big display. Is there a way to specify on load that each form gets displayed maximized on a certain screen so I don't have to drag one form onto the correct screen?

Cheers

Dj
 
You can select Manual for StartPosition property, then set Location property in code. Screen class can be used to give you relevant locations for each screen. Set WindowState property to Maximized in code after location is set.
 
Hi John

Thanks for that it works perfect.

I now have another query, I now have 4 forms in my program so I now use an ini file to tell which 2 forms should be loaded at startup.

VB.NET:
Public Shared Sub main()
            Dim formToShow As System.Windows.Forms.Form = Nothing
            If LoadPrimaryScreen().ToString.Trim = 1 Then
                formToShow = New FrmScreen1
            ElseIf LoadPrimaryScreen().ToString.Trim = 2 Then
                formToShow = New FrmScreen2
            ElseIf LoadPrimaryScreen().ToString.Trim = 3 Then
                formToShow = New FrmScreen3
            ElseIf LoadPrimaryScreen().ToString.Trim = 4 Then
                formToShow = New FrmScreen4
            End If
            Dim screen As Screen
            screen = Windows.Forms.Screen.AllScreens(0)
            formToShow.StartPosition = FormStartPosition.Manual
            formToShow.Location = screen.Bounds.Location + New Point(100, 100)
            formToShow.WindowState = FormWindowState.Maximized
            formToShow.ShowDialog()
            LoadSecondScreen()
 

        End Sub

Public Sub LoadSecondScreen()
        If LoadSecondaryScreen().ToString.Trim = "None" Then
        Else
            Dim Screen2 As System.Windows.Forms.Form = Nothing
            If LoadSecondaryScreen.ToString.Trim = LoadPrimaryScreen.ToString.Trim Then
                MsgBox("There is a StartUp file conflict Primary and Secondary Screens cant be the same")
            ElseIf LoadPrimaryScreen().ToString.Trim = 1 Then
                Screen2 = New FrmScreen1
                Dim SecondaryScreen As Screen
                SecondaryScreen = Windows.Forms.Screen.AllScreens(1)
                Screen2.StartPosition = FormStartPosition.Manual
                Screen2.Location = SecondaryScreen.Bounds.Location + New Point(100, 100)
                Screen2.WindowState = FormWindowState.Maximized
                Screen2.Show()
            ElseIf LoadPrimaryScreen().ToString.Trim = 2 Then
                Screen2 = New FrmScreen2
                Dim SecondaryScreen As Screen
                SecondaryScreen = Windows.Forms.Screen.AllScreens(1)
                Screen2.StartPosition = FormStartPosition.Manual
                Screen2.Location = SecondaryScreen.Bounds.Location + New Point(100, 100)
                Screen2.WindowState = FormWindowState.Maximized
                Screen2.Show()
            ElseIf LoadPrimaryScreen().ToString.Trim = 3 Then
                Screen2 = New FrmScreen3
                Dim SecondaryScreen As Screen
                SecondaryScreen = Windows.Forms.Screen.AllScreens(1)
                Screen2.StartPosition = FormStartPosition.Manual
                Screen2.Location = SecondaryScreen.Bounds.Location + New Point(100, 100)
                Screen2.WindowState = FormWindowState.Maximized
                Screen2.Show()
            ElseIf LoadPrimaryScreen().ToString.Trim = 4 Then
                Screen2 = New FrmScreen4
                Dim SecondaryScreen As Screen
                SecondaryScreen = Windows.Forms.Screen.AllScreens(1)
                Screen2.StartPosition = FormStartPosition.Manual
                Screen2.Location = SecondaryScreen.Bounds.Location + New Point(100, 100)
                Screen2.WindowState = FormWindowState.Maximized
                Screen2.Show()
            End If
        
        End If
    End Sub

Unfortuantly my code doesnt work correctly, as its display the first form perfectly on the correct screen, but it doesnt display the second form until the first is closed down, I need both to be visable at startup.
 
From your Main show the secondary screen first, then the primary using the Application.Run(form) method.
 
Hi All

Ok got another problem with my forms, one of the screens works with multiple threads. If I load the screen by selecting it as the Start Up object all the threads work correctly and the form does what it is meant to do, but if I run the form from within the Sub Main above the screen loads, no errors but none of the threads work, is this beacuse I am loading an instance of the form and not loading the form as a main? Is there a way around this?

Cheers

DJ
 
There is no difference in you calling Application.Run or the application framework doing it for you.
 
Hi JohnH

Ok thanks for that, just a little confused although I have only started looking at threads so it may be my coding, but it works perfectly when the form using the threads is set as the default start up form but doesnt work at all when using the sub main.

Cheers

DJ
 
Hi John similar problem

I have a need to display the same form on 2 screens my codeworks except the form on the pc is shown only in the task bar only I cant physically view t even if I click on it.
<code>

PublicSub ShowOnBothScreens()
Try
Dim sc() AsScreen
sc = Screen.AllScreens
'get all the screen width and heights
frmDisplay.Left = sc(1).Bounds.Width
frmDisplay.Top = sc(1).Bounds.Height
Dim intX AsInteger = sc(1).Bounds.Width - 10
Dim intY AsInteger = sc(1).Bounds.Height - 40
frmDisplay.Width = intX
frmDisplay.Height = intY
frmDisplay.StartPosition = FormStartPosition.Manual
frmDisplay.Height = sc(1).WorkingArea.Height
Dim p AsPoint = NewPoint(sc(1).Bounds.Location.X, sc(1).Bounds.Location.Y)
frmDisplay.Location = p
'Show On The The PC Screen Also
Dim screen2 AsScreen
' We want to display a form on screen 1
screen2 = Screen.AllScreens(1)
' Set the StartPosition to Manual otherwise the system will assign an automatic start position
frmDisplay.StartPosition = FormStartPosition.Manual
' Set the form location so it appears at Location (100, 100) on the screen 1
frmDisplay.Location = screen.Bounds.Location + NewPoint(100, 100)
Catch ex AsException
Finally
frmDisplay.Show()
EndTry
EndSub

</Code>
 
You can't display the same instance of a form on two monitors at the same time (except if you duplicate screens by graphics adapter), a window can only have one location and size, but you can create and show two instances of the same form class.
 
I have tried
dim f as new form
f = frmdisplay
and show f on the pc screen but couldn't get it to work
i assume that is what you are referring to am I missing something here?
 
f = frmdisplay
This does not create a new "frmdisplay", is just assigns the existing one to you variable.
 
Back
Top