Click here to Skip to main content
15,742,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having some trouble setting up my SetDesktopLocation coordinates for my program, specifically the X-position. It's supposed to be used in a dual screen setup where it will open on the 2nd monitor/TV.

The X-position value is stored in a separate XML Configuration File, along with SQL connection string and other values which changes that changes the program's label text. It can be changed by opening the file as Notepad document, then edit and save.

Here's the code snippet for SetDesktopLocation:

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


'Xpos is the name for X-position value'          
Me.SetDesktopLocation(ConfigurationSettings.AppSettings.Get("Xpos"), 0) 
Me.Location = New Point(ConfigurationSettings.AppSettings.Get("Xpos"), 0)
Me.WindowState = FormWindowState.Maximized

End Try

End Sub

In the XML Configuration File, Xpos value is 1280. But when I debug the program, it still opens on my 1st screen.

What I have tried:

I first thought maybe the code couldn't read the XML Config File, so I tested by changing the label text reference to Xpos value. The label can show the Xpos value - 1280, fine. Also the SQL connection string is still working fine.

Then I tried setting the coordinates directly with numbers alone, thinking maybe the original code couldn't read Xpos as value:

Me.SetDesktopLocation(1280, 0) 
Me.Location = New Point(1280, 0)
Me.WindowState = FormWindowState.Maximized

That didn't work too.
Updated 16-Mar-23 14:54pm

1 solution

Why are you using both SetDesktopLocation and Location? They both do the same thing in subtly different ways:"

1. SetDesktopLocation[^]
Sets the location of the form in desktop coordinates.

2. Location[^]
The Point that represents the upper-left corner of the Form in screen coordinates.

Choose the right one, do not use both.

For multiple Screens you need to work with the Screen Class (System.Windows.Forms) | Microsoft Learn[^]

You can check the number of screens available:
Dim screenCount = Screen.AllScreens.Count()

Then you can set the location in the approriate screen based on the number of screens available:
Dim x = 200
Dim y = 200

Dim screenLocation = Screen.AllScreens(screenCount - 1).WorkingArea.Location
Me.Location = New Point(x + screenLocation.X, y + screenLocation.Y)


I do not have a second screen to test with, however, looking at Screen.Bounds Property (System.Windows.Forms) | Microsoft Learn[^], an alternative would be to set the Form.DesktopBounds Property (System.Windows.Forms) | Microsoft Learn[^].
Dim x = 200
Dim y = 200

Dim monitor = Screen.AllScreens _
        .Where(Function(scr) Not scr.Primary) _

' no secondary?
If monitor Is Nothing Then
    monitor = Screen.AllScreens _
        .Where(Function(scr) scr.Primary) _
End If

Me.DesktopBounds = monitor.WorkingArea
Me.Location = New Point(x, y)

Hope this helps.
Share this answer
Hannes1993 16-Mar-23 21:40pm    
Ok. I will use Location for this one. I tried Screen Class 1st. It says Count is not a member of 'System.Array', so I had to modify it slightly:

Dim screenCount = Screen.AllScreens.ToString()

Dim x = 1280
Dim y = 0
Dim screenlocation = Screen.AllScreens(screenCount - 1).WorkingArea.Location
Me.Location = New Point(x + screenlocation.x, y + screenlocation.y)
Me.WindowState = FormWindowState.Maximized

No changes as before, maybe using ToString doesn't work?

Then for Form.DesktopBounds, it says Where is not a member of 'System Array'.
Graeme_Grant 16-Mar-23 23:46pm    
Ran fine on my system. I would use the updated version. That way, if the secondary monitor is not available, it will default back to the primary. More bounds checking is required, but that is for you to do.
Hannes1993 17-Mar-23 1:50am    
Right. I did explain what happen on the last reply. When declaring monitor variable, I got an error saying 'where' is not a member of 'System.Array'. Unlike the previous code where I knew how to work around it, I don't know the work araound for this one..
Dave Kreskowiak 17-Mar-23 0:15am    
Why are you even calling .ToString on a numeric value when you're going to be doing a little math with it? You cannot do math with a string.

Also, your calls to AppSettings.Get() return strings, not integer values. In VB.NET, it'll do implicit conversions for you, but it is not something you want to get into the habit of. Other languages do not do this.
Hannes1993 17-Mar-23 1:53am    
Noted. I added another variable to convert to integer.

Dim screenCount = Screen.AllScreens.ToString()
Dim xPos = Convert.ToInt32(screenCount.text)

And changed all references to screenCount to xPos. But I still get the same problem.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900