Open form on second monitor

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I have this application (build with vb 2010, .net 4.0) and when I click on the show second screen button, it should open a second form on the second monitor (beamer, or whatever) and when no second monitor available message displayed.
But at this moment the second form opens still on the 1 monitor (parent).

This is the code I use now:
VB.NET:
Dim screen As Screen
'Show second form on second screen
screen = screen.AllScreens(1)
SecondForm.StartPosition = FormStartPosition.Manual
SecondForm.Location = screen.Bounds.Location + New Point(100, 100)
SecondForm.Show()

I've got this piece of code from a website and should work, but it doesn't.

Any idea?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,438
Location
Norway
Programming Experience
10+
There is no specific order in the AllScreens array, you have to inspect Primary property of each screen to find the secondary. For example when you have determined there are at least two screens:
VB.NET:
frm.Bounds = (From scr In Screen.AllScreens Where Not scr.Primary)(0).WorkingArea
 

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
sorry I don't get it, where do I need to put this code (sorry still beginning vb).

I have it now as followed:
VB.NET:
Dim screen As Screen
'Show second form on second screen
screen = screen.AllScreens(1)
SecondForm.Bounds = (From scr In screen.AllScreens Where Not scr.Primary)(0).WorkingArea
SecondForm.StartPosition = FormStartPosition.Manual
SecondForm.Location = screen.Bounds.Location + New Point(100, 100)
SecondForm.Show()
 

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
I tried the following:

Added "Dim form As New Form" above the code posted earlier, then change the secondform into form.
It than works. Why isn't that when I use secondform instead of form?
 

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
This is the code as I have it now:

VB.NET:
Dim form As SecondForm
Dim screen As Screen
' We want to display a form on screen 1
screen = screen.AllScreens(1)
' Set the StartPosition to Manual otherwise the system will assign an automatic start position
form.StartPosition = FormStartPosition.Manual
' Set the form location so it appears at Location (100, 100) on the screen 1
form.Location = screen.Bounds.Location + New Point(100, 100)
' Show the form
form.Show()
Me.Select()

Now I get the following error:
"Object reference not set to an instance of an object."

Referring to "form.StartPosition = FormStartPosition.Manual" of the code above

Don't know whats wrong
 

chrisatpsu

Member
Joined
Feb 26, 2012
Messages
7
Programming Experience
3-5
change this...
screen = screen.AllScreens(1)
to...
screen = screen.AllScreens(0)

in windows settings, you have your secondary set up in software as your "main display"
 
Top Bottom