how i can move a picture in vb

Dani AI

Generated

asked how to move a picture in VB and pointed to a sample. Posting an answer here is more useful than emailing the code: below are short, tested patterns for the common cases (arrow-key movement, mouse drag) for VB.NET WinForms plus a brief VB6 note. Key points: set the form's KeyPreview = True when using keys, do bounds checks to keep the image on screen, and use a Timer for smooth animation.

' VB.NET (WinForms) - move PictureBox with arrow keys
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.KeyPreview = True
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    Dim stepSize As Integer = 5
    Select Case e.KeyCode
        Case Keys.Left
            PictureBox1.Left = Math.Max(0, PictureBox1.Left - stepSize)
        Case Keys.Right
            PictureBox1.Left = Math.Min(Me.ClientSize.Width - PictureBox1.Width, PictureBox1.Left + stepSize)
        Case Keys.Up
            PictureBox1.Top = Math.Max(0, PictureBox1.Top - stepSize)
        Case Keys.Down
            PictureBox1.Top = Math.Min(Me.ClientSize.Height - PictureBox1.Height, PictureBox1.Top + stepSize)
    End Select
End Sub
' VB.NET - drag PictureBox with the mouse (robust: uses screen cursor delta)
Private dragStart As Point
Private picStart As Point
Private dragging As Boolean = False

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    If e.Button = MouseButtons.Left Then
        dragging = True
        dragStart = Cursor.Position
        picStart = PictureBox1.Location
    End If
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If dragging Then
        Dim delta As Point = New Point(Cursor.Position.X - dragStart.X, Cursor.Position.Y - dragStart.Y)
        PictureBox1.Location = New Point(picStart.X + delta.X, picStart.Y + delta.Y)
    End If
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    dragging = False
End Sub
' VB6 - simple arrow-key move (Form.KeyPreview = True). Note: Left/Top are in twips.
Private Sub Form_Load()
    Me.KeyPreview = True
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Const stepSize As Long = 300 ' adjust for twips or change ScaleMode
    Select Case KeyCode
        Case vbKeyLeft: Picture1.Left = Picture1.Left - stepSize
        Case vbKeyRight: Picture1.Left = Picture1.Left + stepSize
        Case vbKeyUp: Picture1.Top = Picture1.Top - stepSize
        Case vbKeyDown: Picture1.Top = Picture1.Top + stepSize
    End Select
End Sub

Troubleshooting notes: if KeyDown does not fire, ensure KeyPreview = True and that a child control is not swallowing keys; if movement is jumpy, use a Timer with a smaller step and lower Interval; in VB6 remember units (twips) or set ScaleMode.

Recommended Answers

All 2 Replies

See this sample :

See this sample :

this prog in work in my pc yy plz write the code amd mail me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.