Reputation: 265
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 screen, 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
Upvotes: 1
Views: 3612
Reputation: 265
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
Upvotes: 0
Reputation: 888067
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;
Upvotes: 1