How to drag a picturebox inside a panel control?

S

Steven Garrad

Hi All,
I have a pictureBox control inside of a panel control. The pictureBox is
larger than the panel control and I have the panel control set true for
AutoScroll so the panel displays scrolling bars.

I would like the user to be able to click and drag the image and for it to
move around within the panel control.

How would I do this?

Cheers,
Steve
 
A

Alex Meleta

Steve,

The simple way to just move the picture by clicking is:

// Define a location before move
int x;
int y;

// Storing begin point (by the left click)
void pictureBox_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
x = e.X;
y = e.Y;
} }

// Moving the picture
void pictureBox_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
pictureBox1.Left += (e.X - x);
pictureBox1.Top += (e.Y - y);
} }

Alex
http://devkids.blogspot.com
 

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