System.Windows.Forms.Screen.AllScreens

  • Thread starter Thread starter Allen
  • Start date Start date
A

Allen

How to force System.Windows.Forms.Screen.AllScreens to update when attaching
an additional monitor?

Form1 is Loaded and runs the following upon detection of additional monitor:

Dim Screens() As System.Windows.Forms.Screen
Screens = System.Windows.Forms.Screen.AllScreens
MsgBox("SWFSAsLength: " & System.Windows.Forms.Screen.AllScreens.Length &
vbCr & "Screens: " & Screens.Length)

Output:
WSFSAsLength: 1
Screens: 1

If a message box is displayed prior to setting the Screens variable such as
following then System.Windows.Forms.Screens.AllScreens is updated as
follows:

Dim Screens() As System.Windows.Forms.Screen
MsgBox("anything")
Screens = System.Windows.Forms.Screen.AllScreens
MsgBox("SWFSAsLength: " & System.Windows.Forms.Screen.AllScreens.Length &
vbCr & "Screens: " & Screens.Length)

Output:
WSFSAsLength: 2
Screens: 2

Need to obtain HxV size of each attached monitor. Is there a better way?
 
It sounds like you want to run the code listed in the help for the Screen
class. As to why you get a different answer after a messagebox, I suggest
putting this in a routine handling the shown event rather than the load
event. It may be that the forms library gets initialized after something is
shown, which in this case, would be your form.
 
Back
Top