Moving Picture Box Using Arrow Keys

A program that enables to move the image to forward, backward , upward and downward inside the form using the arrow keys.

So, first step is to create a form and add a picture object, then upload any picture by navigating in the Image property of the picture object. Name the object picMove in Name properties.

Here’s mine.



Copy and Paste then code


Public Class frmMov
    'VARIABLE DECLARATION
    Dim NewPoint As Point
    Private Sub frmMov_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Up
                NewPoint.Y = picMov.Location.Y - 5
                NewPoint.X = picMov.Location.X
            Case Keys.Left
                NewPoint.Y = picMov.Location.Y
                NewPoint.X = picMov.Location.X - 5
            Case Keys.Down
                NewPoint.Y = picMov.Location.Y + 5
                NewPoint.X = picMov.Location.X
            Case Keys.Right
                NewPoint.Y = picMov.Location.Y
                NewPoint.X = picMov.Location.X + 5
        End Select
        picMov.Location = NewPoint
    End Sub
End Class

Final Output

Moving Picture Box Using Arrow Keys Moving Picture Box Using Arrow Keys Reviewed by code-dev on 4:30 PM Rating: 5

No comments:

Powered by Blogger.