碰撞:图片框,但只有顶部和底部

时间:2016-10-13 06:51:44

标签: vb.net picturebox

我有两个图片盒,另一个不能触及它的右边和左边。有可能吗?

1 个答案:

答案 0 :(得分:0)

您可以检查图片框是否覆盖了Location.XLocation.Y属性。

要将一个图片框翻转到顶部或底部,请使用SetBounds(x,y,width,height)方法。

示例:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim xw = PictureBox1.Location.X + PictureBox1.Width
        Dim x2w = PictureBox2.Location.X + PictureBox2.Width

        If PictureBox2.Location.X <= xw AndAlso x2w < xw Then 'PictureBox2 touches right side of PictureBox1 => flip to bottom'
            movePictureBox(PictureBox1, PictureBox2, PictureBox1.Height + 5)
        ElseIf PictureBox2.Location.X <= xw AndAlso x2w >= xw Then 'PB2 touches left side of PB1 => flip to top'
            movePictureBox(PictureBox1, PictureBox2, -PictureBox2.Height - 5)
        End If
 End Sub

Private Sub movePictureBox(picFrom As PictureBox, picToMove As PictureBox, yOffset As Integer)
        'Set new location for picToMove according to picFrom.X and picFrom.Y + offset'
        picToMove.SetBounds(picFrom.Location.X, picFrom.Location.Y + yOffset, picToMove.Width, picToMove.Height)
End Sub