I ran into this scenario recently, and try as I might I could not find a good code example that moved a form to a specific desktop screen. Perhaps it's because the solution is so simple when you look at it.

In my situation, I have an application that creates a form for each production monitor that is located throughout the plant. Each of these monitors connect directly to the main computer as an extended desktop. Each form is customized for the area in which the monitor is located.

Using something similar to the code below, you can specifically set which monitor your form will display on:



vb.net Code:
  1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  2.  
  3.      ' THIS USES THE SCREEN CLASS TO GET THE COLLECTION OF SCREENS ON THE COMPUTER
  4.      For Each View As Screen In System.Windows.Forms.Screen.AllScreens()
  5.          
  6.           ' EACH SCREEN HAS A DEVICE NAME SIMILAR TO \\.\DISPLAY1
  7.           Dim str As String = View.DeviceName
  8.  
  9.           ' WE WANT TO GET THE NUMBER AT THE END OF THE DEVICENAME
  10.           ' I ENTERED THE NUMBER 2 AS A STRING, BUT THIS COULD EASILY BE SET IN A CONFIG FILE OR DATABASE
  11.           If str.EndsWith("2") Then
  12.  
  13.                ' IN ORDER TO MOVE THE FORM'S LOCATION ON YOUR DESKTOP, CALL THE SETDESKTOPLOCATION METHOD
  14.                Me.SetDesktopLocation(View.WorkingArea.Left, View.WorkingArea.Top)
  15.  
  16.                ' IN MY CASE, I WANT THE FORM TO BE MAXIMIZED ON THE SCREEN
  17.                ' YOU CAN EASILY CUSTOMIZE THE SIZE OF YOUR FORM BY USING THE PROPERTIES OF VIEW.WORKINGAREA
  18.                ' ALTERNATELY, YOU CAN USE VIEW.BOUNDS
  19.                Me.WindowState = FormWindowState.Maximized
  20.  
  21.                ' NO NEED TO CYCLE THROUGH THE OTHER SCREENS IF WE FOUND THE RIGHT ONE
  22.                Exit For
  23.  
  24.           End If
  25.  
  26.      Next
  27.  
  28. End Sub