Results 1 to 4 of 4

Thread: Drag Drop

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Resolved Drag Drop

    How can I make it so an object is movable by the user? Example: I click a picture box and drag it from one corner to the other. I've been looking up examples for a while and only find file and text drag/drop.
    Last edited by bluehairman; Dec 16th, 2008 at 06:14 PM. Reason: [RESOLVED]

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Drag Drop

    You can use the MouseDown and MouseUp events (of the picturebox) to set a flag to true (on MouseDown) and to False (on MouseUp). Then in the MouseMove event, if the flag is True, set the picturebox location to the mouse location.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Drag Drop

    Ok, I have:
    vb Code:
    1. Private Sub MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    2.         MDown = True
    3.     End Sub
    4.  
    5.     Private Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    6.         If MDown = True Then
    7.             MyBase.Item(IndexOf(sender)).Location = e.Location
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    12.         If MDown = True Then
    13.             MyBase.Item(IndexOf(sender)).Location = e.Location
    14.             MDown = False
    15.         End If
    16.     End Sub
    It works, but I see the image everywhere when I'm dragging (In the path I'm dragging), and when I drop it drops way off where I drop it.
    EDIT: Theres actually 2 paths, and the path that the image is really following is random.
    Last edited by bluehairman; Dec 16th, 2008 at 05:18 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Re: Drag Drop

    Got it working with:
    vb Code:
    1. Private Sub MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    2.         MDown = True
    3.         Starter = e.Location
    4.     End Sub
    5.  
    6.     Private Sub MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    7.         If MDown = True Then
    8.             MyBase.Item(IndexOf(sender)).Top = MyBase.Item(IndexOf(sender)).Top + _
    9.    (e.Location.Y - Starter.Y)
    10.             MyBase.Item(IndexOf(sender)).Left = MyBase.Item(IndexOf(sender)).Left + _
    11.                (e.Location.X - Starter.X)
    12.  
    13.         End If
    14.     End Sub
    15.  
    16.     Private Sub MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    17.         MDown = False
    18.     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