Reputation: 60892
i need to be able to drag and drop my picturebox with an image in it around my winform in vb.net.
Upvotes: 1
Views: 13539
Reputation: 144
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.
Upvotes: 0
Reputation: 2731
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
Upvotes: 1
Reputation: 68516
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;
}
Upvotes: 6