Search code examples
vb.netwinformsmultiple-monitors

Run VB.net app on second monitor


I'm a newbie at coding but I have done simple application for the plan I work for!!

I recently encounter a "little" issue with one of my application!

I build a Windows Form application, with multiple form, that run on a dual screen CNC. The CNC program have to run on the primary screen at a higher resolution. My application run on the second screen who is a touch screen at 1024;768.

The problem itself is if I run the code with the debug data everything run as I want, open the application on second screen and all the next form open on this screen. If I install it with the released data, all form open on the primary monitor, even if I drag them on second screen. After I closed them, they return on the primary.

Is there a way I can put a code line at the beginning of each form who make all form open on the secondary screen.

When the setup will work correctly I'll lock the screen setup just to be sure nobody will mess with the settings.

Please be gentle with me I don't have any formation on how to code. I learn from myself with reading on the web!!

Thanks all!!


Solution

  • Something like this should position the window at offset 100, 100 on the first non-primary screen found. You can adjust the location and/or size to suit your needs.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim secondaryMonitor = Screen.AllScreens.FirstOrDefault(Function(x) Not x.Primary)
        If secondaryMonitor IsNot Nothing Then
            Dim newLocation = secondaryMonitor.Bounds.Location
            newLocation.Offset(100, 100) ' adjust as needed
            Me.Location = newLocation
            ' Also see Me.Size and Me.Bounds
        End If
    End Sub