Results 1 to 6 of 6

Thread: [2005] Moving the form with a picturebox?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    146

    [2005] Moving the form with a picturebox?

    Okay, so just wondering, if its possible. I Have a picturebox (PictureBox1) and just wondering if I dragged the picturebox how could i drag the form?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [2005] Moving the form with a picturebox?

    this works

    Code:
    ' Tracks whether the form is in drag mode. If it is, mouse movements
        ' over the picturebox will be translated into form movements.
        Dim Dragging As Boolean
    
        ' Stores the offset where the picturebox is clicked.
        Dim PointClicked As Point
    
        Private Sub picturebox1_MouseDown(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) Handles picturebox1.MouseDown
    
            If e.Button = MouseButtons.Left Then
                Dragging = True
                PointClicked = New Point(e.X, e.Y)
            Else
                Dragging = False
            End If
    
        End Sub
    
        Private Sub picturebox1_MouseMove(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) Handles picturebox1.MouseMove
    
            If Dragging Then
                Dim PointMoveTo As Point
    
                ' Find the current mouse position in screen coordinates.
                PointMoveTo = Me.PointToScreen(New Point(e.X, e.Y))
    
                ' Compensate for the position the control was clicked.
                PointMoveTo.Offset(-PointClicked.X, -PointClicked.Y - (Me.Height - Me.ClientRectangle.Height))
                ' Move the form.
                Me.Location = PointMoveTo
            End If
    
        End Sub
    
        Private Sub picturebox1_MouseUp(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.MouseEventArgs) Handles picturebox1.MouseUp
    
            Dragging = False
    
        End Sub
    Last edited by .paul.; Oct 12th, 2007 at 03:25 PM. Reason: improvement

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    146

    Re: [2005] Moving the form with a picturebox?

    Thanks! Ill try it now

    EDIT: Thanks so much Ive been trying to do this for ages!
    Last edited by Ashylay; Oct 13th, 2007 at 05:26 AM.

  4. #4
    Member
    Join Date
    Dec 2008
    Posts
    49

    Re: [2005] Moving the form with a picturebox?

    Hey, thanks .Paul, it worked great for me :-)

  5. #5
    Lively Member
    Join Date
    Feb 2008
    Posts
    91

    Re: [2005] Moving the form with a picturebox?

    Don't forget to mark this thread "resolved".

    Thank you Paul.

  6. #6
    Addicted Member Mal1t1a's Avatar
    Join Date
    Mar 2008
    Posts
    157

    Re: [2005] Moving the form with a picturebox?

    Smaller/Compressed Code (Easy for memorization):
    Code:
        Dim Moveable As Boolean
        Dim LastPos As Point
        Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            Moveable = True
            LastPos = Cursor.Position - Me.Location
        End Sub
        Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
            If Moveable Then
                Me.Location = Cursor.Position - LastPos
            End If
        End Sub
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
            Moveable = False
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width