Moving a pictureBox in a panel

I have a project in C #, WindowsForms, and I created a panel that contains a pictureBox , which is much larger than its parent.

I turned panel.AutoScroll to true , and I want to make this pictureBox in panel instead of catching the scroll and moving it.

those. when I grab the image and move the cursor left and down, I would like to get the same behavior as me by doing this with the scroll panel .

How to do it?

+4
source share
3 answers

Ok, I get it. ;-) If anyone else has the same problem, here is the solution:

  protected Point clickPosition; protected Point scrollPosition; private void pictureBox_MouseDown(object sender, MouseEventArgs e) { this.clickPosition.X = eX; this.clickPosition.Y = eY; } private void pictureBox_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { scrollPosition.X = scrollPosition.X + clickPosition.X - eX; scrollPosition.Y = scrollPosition.Y + clickPosition.Y - eY; this.panel.AutoScrollPosition = scrollPosition; } } 
+5
source

smaller solution hsz :)

  protected Point clickPosition; protected Point scrollPosition; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { this.clickPosition = e.Location; } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.SuspendLayout(); this.scrollPosition += (Size)clickPosition - (Size)e.Location; this.panel1.AutoScrollPosition = scrollPosition; this.ResumeLayout(false); } } 
0
source

hsz 'improved solution, with scroll restriction, but I only allow vertical scrolling

 protected Point clickPosition; protected Point scrollPosition; private void picBoxScan_MouseDown(object sender, MouseEventArgs e) { this.clickPosition.X = eX; this.clickPosition.Y = eY; } private void picBoxScan_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { scrollPosition.X = panelViewFile.AutoScrollPosition.X; scrollPosition.Y = scrollPosition.Y + (clickPosition.Y - eY); scrollPosition.Y = Math.Min(scrollPosition.Y,panelViewFile.VerticalScroll.Maximum); scrollPosition.Y = Math.Max(scrollPosition.Y,panelViewFile.VerticalScroll.Minimum); panelViewFile.AutoScrollPosition = scrollPosition; } } 
0
source

All Articles