Dragging an image in Visual Basic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alkire
    New Member
    • May 2007
    • 7

    #1

    Dragging an image in Visual Basic

    how do i move a image by clicking holding moving letting go and then the image moves to the new spot

    what is the code for vb6
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by alkire
    how do i move a image by clicking holding moving letting go and then the image moves to the new spot

    what is the code for vb6
    The absolute simplest way is to set the picturebox control's .DragMode property to "1 - Automatic". That will allow you to drag it around, but won't do anything when you drop it.

    Then, if you add this code to the form...
    Code:
    Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
      Source.Move X, Y
    End Sub
    it will be moved.

    The result is ugly, because it always works as though you grabbed the top-left corner, but it's a start.

    I'll just give you a hint on how to fix it. Track where the mouse was when you grabbed the picture, then use that offset to adjust the place where you drop it.

    Comment

    • brazel
      New Member
      • May 2007
      • 11

      #3
      Good day!

      Try this code in vb

      [CODE=vb]Dim b, xx, yy As Integer

      Private Sub Form_Load()
      b = 0
      End Sub


      Private Sub Picture1_MouseD own(Button As Integer, Shift As Integer, x As Single, y As Single)
      b = 1
      End Sub

      Private Sub Picture1_MouseM ove(Button As Integer, Shift As Integer, x As Single, y As Single)
      If b = 0 Then
      xx = x
      yy = y
      End If

      If b = 1 Then
      Picture1.Left = Picture1.Left + x - xx
      Picture1.Top = Picture1.Top + y - yy
      End If
      End Sub

      Private Sub Picture1_MouseU p(Button As Integer, Shift As Integer, x As Single, y As Single)
      b = 0
      End Sub[/CODE]
      Last edited by Killer42; May 16 '07, 08:43 AM. Reason: Added [CODE=vb] tag

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I'd just like to point out a couple of things which are not exactly bugs, but which I believe demonstrate a bit of a lack of understanding of how VB works. (I could be wrong).

        Dim b, xx, yy As Integer
        In VB6 (and earlier), this only defines yy as an Integer. The others will be of the default type, which is most likely to be Variant.

        Private Sub Form_Load()
        b = 0
        End Sub

        This is completely unnecessary. b is already initialised to zero when it's created. And as a form-level variable it will be destroyed if the form is unloaded, so it can't have a value left over from before.

        Comment

        • brazel
          New Member
          • May 2007
          • 11

          #5
          Yes! Thanks.

          Private Sub Form_Load()
          b = 0
          End Sub

          This code unnecessary.

          Comment

          Working...