开发者

Display 2 Forms, side by side

开发者 https://www.devze.com 2023-01-10 23:17 出处:网络
I would like to display 2 forms side by side.Frm1 will call Frm2. The problem is, if Frm1 is too near the edge of the computer scr开发者_开发百科een, when Fr2 is shown, part of it will be outside the

I would like to display 2 forms side by side. Frm1 will call Frm2. The problem is, if Frm1 is too near the edge of the computer scr开发者_开发百科een, when Fr2 is shown, part of it will be outside the screen.

How do I make it so that if Frm1 is near the right-side of the screen then Frm2 will show on the left-side of Frm1 and vise-versa? Thanks


Assuming WinForms, you can compare form.Bounds to Screen.FromControl(form).WorkingArea.

For example:

var screen = Screen.FromControl(form);
if (form.Right < screen.Left + Screen.Width / 2)
    otherForm.Left = screen.Left + Screen.Width / 2;
else
    otherForm.Left = 0;


finally got it working, here is the solution:

 Public Class Form1
    Public IsFormLeft As Boolean
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm1ScreenArea = Screen.FromControl(Me).WorkingArea

        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.ShowDialog()
    End Sub
End Class


Public Class Form2
       Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Form1.IsFormLeft Then
            Me.Left = Form1.Left + Form1.Width
            Me.Top = Form1.Top
        Else
            Me.Left = Form1.Right - Form1.Width * 2
            Me.Top = Form1.Top
        End If
    End Sub
End Class
0

精彩评论

暂无评论...
验证码 换一张
取 消