dragging a picture box

D

dk60

I want to allow the user of my application to drag a picture box
around, so I wrote these simple event handlers:
private void pictureBox1_MouseDown(object sender,
MouseEventArgs e)
{
pictureBox1Movable = true;
}

private void pictureBox1_MouseMove(object sender,
MouseEventArgs e)
{
if (pictureBox1Movable==true)
{
Point p = pictureBox1.Location;
p.X += e.X;
p.Y += e.Y;
pictureBox1.Location = p;
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs
e)
{
pictureBox1Movable = false;
}

However, when the user starts dragging, the picture box "jumps" so
that the upper left corner coincides with the mouse pointer and after
that the dragging goes fine. I cannot find a way to fix it. I tried
several ideas but none works. This looks like a simple matter that
should have a straightforward solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top