禁用form2移出form1

时间:2015-03-14 08:51:24

标签: vb.net forms

我有两种形式。 Form1和Form2

Form1是600x500,form2是300x250。

Form1总是打开,当我打开form2时,它应该在form1之上。如何禁用form2移出宽度form1

喜欢这个 enter image description here

不喜欢这样 enter image description here

所以它应该是可重复的,但无法移出边缘form1。

1 个答案:

答案 0 :(得分:1)

如果您不打算使用 MDI Forms

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.TopMost = True
    Me.Left = Form1.Location.X
    Me.Top = Form1.Location.Y + 30
End Sub

Private Sub Form2_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
    'check position
    Dim topLeft, topRight, bottomLeft, BottomRight As Point
    With Form1
        topLeft = New Point(.Location.X, .Location.Y + 30)
        topRight = New Point(.Location.X + .Width, .Location.Y + 30)
        bottomLeft = New Point(.Location.X, .Location.Y + .Height)
        'BottomRight = New Point(.Location.X + .Location.Y + .Height)
    End With
    Dim mytopLeft, mytopRight, mybottomLeft, myBottomRight As Point
    With Me
        mytopLeft = New Point(.Location.X, .Location.Y)
        mytopRight = New Point(.Location.X + .Width, .Location.Y)
        mybottomLeft = New Point(.Location.X, .Location.Y + .Height)
        'myBottomRight = New Point(.Location.X + .Location.Y + .Height)
    End With

    If topLeft.X > mytopLeft.X Then
        Me.Location = New Point(topLeft.X, Me.Location.Y)
        Me.Text = "topLeftX"
    End If
    If topLeft.Y > mytopLeft.Y Then
        Me.Location = New Point(Me.Location.X, topLeft.Y)
        Me.Text = "topLeftY"
    End If

    If topRight.X < mytopRight.X Then
        Me.Location = New Point(topRight.X - Me.Width, Me.Location.Y)
        Me.Text = "topRightX"
    End If

    If bottomLeft.Y < mybottomLeft.Y Then
        Me.Location = New Point(Me.Location.X, bottomLeft.Y - Me.Height)
        Me.Text = "bottomLeftY"
    End If
End Sub