1

i need to be able to drag and drop my picturebox with an image in it around my winform in vb.net.

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • By drag and drop I'm assuming you actually meant just moving the image? Dragging and dropping is something else entirely in my opinion. – djdd87 Jul 06 '09 at 13:44

3 Answers3

6

This is in C#, but should be easy enough to replicate in VB.Net.

private int   currentX, currentY;
private bool  isDragging = false;

private void myPictureBox_MouseDown(object sender, MouseEventArgs e) 
{
  isDragging = true;

  currentX = e.X;
  currentY = e.Y;
}

private void myPictureBox_MouseMove(object sender, MouseEventArgs e) 
{
  if (isDragging) 
  {
    myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
    myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
  }
}

private void myPictureBox_MouseUp(object sender, MouseEventArgs e) 
{
  isDragging = false;
}
djdd87
  • 67,346
  • 27
  • 156
  • 195
  • btw your example is much more elegant and simpler than microsoft (which doesnt even work with picturebox) http://msdn.microsoft.com/en-us/library/ms973845.aspx – Alex Gordon Jul 06 '09 at 13:49
  • I question whether or not this code works as it does not convert from the picturebox client coordinate space to a common coordinate space (screen space). – user79755 Jul 06 '09 at 14:16
  • 1
    Agreed, it wont work when the PictureBox is in a Panel or another container, however he asked for code that worked with a PictureBox on a WinForm, so that's what I gave him. – djdd87 Jul 06 '09 at 14:35
  • 1
    Hi Alex, the code does work, however when you put the PictureBox inside a container, i.e. a Panel, you'll have to take the Position of the panel in relation to the Form into consideration. – djdd87 Jul 07 '09 at 07:45
  • gotcha - fine then, can you show me how i would do it within a panel? – Alex Gordon Jul 07 '09 at 10:00
  • 1
    Neodymium had me doubting the code. I knew of should of tested it within a Panel before saying it wouldn't work! – djdd87 Jul 07 '09 at 10:38
  • did you just write this code for me or did you get it from elsewhere? – Alex Gordon Jul 07 '09 at 10:46
  • 1
    I wrote it for an old project a long time ago, so I just copied and pasted it from there. However the chances are I probably based it on someone else's solution. – djdd87 Jul 07 '09 at 13:12
1

Here's some VB.NET


    Private IsDragging As Boolean = False
    Private StartPoint As Point

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        StartPoint = PictureBox1.PointToScreen(New Point(e.X, e.Y))
        IsDragging = True
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If IsDragging Then
            Dim EndPoint As Point = PictureBox1.PointToScreen(New Point(e.X, e.Y))            
            PictureBox1.Left += (EndPoint.X - StartPoint.X)
            PictureBox1.Top += (EndPoint.Y - StartPoint.Y)
            StartPoint = EndPoint
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        IsDragging = False
    End Sub
user79755
  • 2,623
  • 5
  • 30
  • 36
  • this is also very good but i was wondering if you knew the answer to this question; http://stackoverflow.com/questions/1087130/moving-image-on-web-page – Alex Gordon Jul 06 '09 at 15:19
0

Code similar to the answers provided exist in this DreamInCode.com thread. Another thing the thread addresses is keeping the picturebox within the bounds of the form and resizing the picturebox.

Jamie L.
  • 144
  • 1
  • 10