Ir para o conteúdo

Forms (side-by-side)

O código apresentado permite ter a Form2 (neste caso) sempre "colada" à Form1, seja do lado direito ou lado esquerdo, consoante a posição da Form1.

Public Class Form1
    Public IsFormLeft As Boolean
    Dim frm1ScreenArea = Screen.FromControl(Me).WorkingArea

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


        If Me.Right < frm1ScreenArea.Left + frm1ScreenArea.Width / 2 Then
            IsFormLeft = True 'Form1 in Left area
        Else
            IsFormLeft = False 'Form1 in Right area
        End If
        Form2.Show()

    End Sub

    Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
        Form2.BringToFront()

        If Me.Right < frm1ScreenArea.Left + frm1ScreenArea.Width / 2 Then
            IsFormLeft = True 'Form1 in Left area
        Else
            IsFormLeft = False 'Form1 in Right area
        End If

        If Me.IsFormLeft Then
            Form2.Left = Me.Left + Me.Width
            Form2.Top = Me.Top
        Else
            Form2.Left = Me.Left - Form2.Width
            Form2.Top = Me.Top
        End If
    End Sub
End Class