I am having difficulties with getting a picture box to not go off the form in any of the 4 directions (left, right, up, down) using a Key Down Event. I have sourced a useful function from this site but am still having a bit of difficulty. This is what I have so far: The function looks great but I am not sure where to put it in my location changed code.
PublicClass Form1
Dim vx As Single = 1.0
Dim vy As Single = 1.0
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Up
PictureBox1.Top -= 10
Case Keys.Down
PictureBox1.Top += 10
Case Keys.Right
PictureBox1.Left += 10
Case Keys.Left
PictureBox1.Left -= 10
End Select
End Sub
Private Function moveIsInBounds(ByVal futurePosition As Point, ByVal objMoving As Size, ByVal container As Size) As Boolean
Dim xInBounds As Boolean = (futurePosition.X >= 0) AndAlso (futurePosition.X + objMoving.Width <= container.Width)
Dim yInBounds As Boolean = (futurePosition.Y >= 0) AndAlso (futurePosition.Y + objMoving.Height <= container.Height)
If xInBounds AndAlso yInBounds Then
Return True
Else
Return False
End If
End Function
Private Sub picturebox1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.LocationChanged
Dim l As Integer = PictureBox1.Left + vx
Dim t As Integer = PictureBox1.Top + vy
If (l <= 0) Or (l + PictureBox1.Width >= Me.ClientSize.Width) Then
vx = -vx 'reverse X velocity
Else
PictureBox1.Left = 1
End If
If (t <= 0) Or (t + PictureBox1.Height >= Me.ClientSize.Height) Then
vy = -vy 'reverse Y velocity
Else
PictureBox1.Top = t
End If
End Sub
EndClass
Any assistance would be appreciated.Thanks
PublicClass Form1
Dim vx As Single = 1.0
Dim vy As Single = 1.0
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Up
PictureBox1.Top -= 10
Case Keys.Down
PictureBox1.Top += 10
Case Keys.Right
PictureBox1.Left += 10
Case Keys.Left
PictureBox1.Left -= 10
End Select
End Sub
Private Function moveIsInBounds(ByVal futurePosition As Point, ByVal objMoving As Size, ByVal container As Size) As Boolean
Dim xInBounds As Boolean = (futurePosition.X >= 0) AndAlso (futurePosition.X + objMoving.Width <= container.Width)
Dim yInBounds As Boolean = (futurePosition.Y >= 0) AndAlso (futurePosition.Y + objMoving.Height <= container.Height)
If xInBounds AndAlso yInBounds Then
Return True
Else
Return False
End If
End Function
Private Sub picturebox1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.LocationChanged
Dim l As Integer = PictureBox1.Left + vx
Dim t As Integer = PictureBox1.Top + vy
If (l <= 0) Or (l + PictureBox1.Width >= Me.ClientSize.Width) Then
vx = -vx 'reverse X velocity
Else
PictureBox1.Left = 1
End If
If (t <= 0) Or (t + PictureBox1.Height >= Me.ClientSize.Height) Then
vy = -vy 'reverse Y velocity
Else
PictureBox1.Top = t
End If
End Sub
EndClass
Any assistance would be appreciated.Thanks