views:

661

answers:

2

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

+2  A: 

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;
}
GenericTypeTea
this is absolutely brilliant thank you very much
I__
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
I__
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).
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.
GenericTypeTea
?? whats the discussion about? this code works!
I__
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.
GenericTypeTea
gotcha - fine then, can you show me how i would do it within a panel?
I__
Actually, scrap that, it works fine within a Panel.
GenericTypeTea
you're the man!
I__
Neodymium had me doubting the code. I knew of should of tested it within a Panel before saying it wouldn't work!
GenericTypeTea
did you just write this code for me or did you get it from elsewhere?
I__
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.
GenericTypeTea
+1  A: 

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
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
I__