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
2009-07-06 13:39:02
this is absolutely brilliant thank you very much
I__
2009-07-06 13:48:27
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__
2009-07-06 13:49:37
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).
2009-07-06 14:16:01
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
2009-07-06 14:35:58
?? whats the discussion about? this code works!
I__
2009-07-06 15:20:24
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
2009-07-07 07:45:11
gotcha - fine then, can you show me how i would do it within a panel?
I__
2009-07-07 10:00:54
Actually, scrap that, it works fine within a Panel.
GenericTypeTea
2009-07-07 10:17:01
you're the man!
I__
2009-07-07 10:22:08
Neodymium had me doubting the code. I knew of should of tested it within a Panel before saying it wouldn't work!
GenericTypeTea
2009-07-07 10:38:03
did you just write this code for me or did you get it from elsewhere?
I__
2009-07-07 10:46:55
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
2009-07-07 13:12:43
+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__
2009-07-06 15:19:41