1

This question is related to another question of mine which can be found here can be found here. I wanted to move a PictureBox within its parent container which is a TabPage (If it does make any difference!) Using the code below the movement can be done:

private Point start = Point.Empty; 
private bool _mapPackageIsMoving;    

void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e) { 
  _mapPackageIsMoving = false; 
} 

void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) { 
  if (_mapPackageIsMoving) { 
    pictureBoxPackageView.Location = new Point( 
                             pictureBoxPackageView.Left + (e.X - start.X),  
                             pictureBoxPackageView.Top + (e.Y - start.Y)); 
  } 
} 

void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e) { 
  start = e.Location; 
  _mapPackageIsMoving = true; 
} 

Now my problem is, There is no limit to this moving of control. User can drag the controls kilometers away from the visible area of the TabPage which my picturebox is inside it. I tried to add some limits for movement by changing the MouseMove event like this, to atleast prevent it from going out of the Left and Right visible area of the tabpage:

void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) { 
  if (_mapPackageIsMoving) { 
   //Added condition
  if (pictureBoxPackageView.Left >= 0 && pictureBoxPackageView.Right >= 0)
    pictureBoxPackageView.Location = new Point( 
                             pictureBoxPackageView.Left + (e.X - start.X),  
                             pictureBoxPackageView.Top + (e.Y - start.Y)); 
  } 
} 

But the problem with the code above is whenever the picturebox hits the right or left side of the container and the Left or Right get equal to 0, I can not move the control anymore.

Any helps/tips to achive limiting this movement inside the container for Left, Right, Top and Bottom of the picture box will be appriciated!

Community
  • 1
  • 1
Dumbo
  • 13,555
  • 54
  • 184
  • 288

2 Answers2

3

you can move the box unconditionally (no testing of the current location) and have a limitation for your new location:

int nx = Math.Min(Math.Max(pictureBoxPackageView.Left + (e.X -start.X),0),pictureBoxPackageView.Parent.Width-pictureBoxPackageView.Width);
int ny = Math.Min(Math.Max(pictureBoxPackageView.Top + (e.Y -start.Y),0),pictureBoxPackageView.Parent.Height-pictureBoxPackageView.Height);

pictureBoxPackageView.Location = new Point(nx,ny);
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
  • Thanks! This works like a charm. Would you mind telling me one more thing? How can I let the control move through the `Bottom` of the container? I mean limit shouldnt apply for Bottom but still apply to left, right and top. – Dumbo Aug 17 '12 at 08:02
  • Never mind, found it as `var y = Math.Min(Math.Max(finalLocation.Y, 0), pictureBoxPackageView.Bottom);` – Dumbo Aug 17 '12 at 08:08
  • there are typo here.. `pictureBoxPackageView.parent` should be `pictureBoxPackageView.Parent` – aswzen Sep 07 '16 at 10:30
-2

I think if you add the following code, it will move the item without snapping to the top as an option.

//- MouseDownLocation.X
 //- MouseDownLocation.Y 

       int nx = Math.Min(Math.Max(label1.Left - MouseDownLocation.X + (e.X - start.X), 0), label1.Parent.Width - label1.Width);
       int ny = Math.Min(Math.Max(label1.Top - MouseDownLocation.Y + (e.Y - start.Y), 0), label1.Parent.Height - label1.Height);
ACopeLan
  • 154
  • 1
  • 8
  • Why you repeat an answer to a very old topic? Here for some quick points?! – Dumbo May 05 '17 at 19:54
  • 1
    I think we are bit over-sensitive much. I don't know anything about points and wasn't aware that being helpful was a game. Is this some sort of game that I'm not aware of? I was just trying to be helpful. But that may explain why everyone seems so eager to not help. Lol I would ask a question and get negative points. Is this a gaming site?? – ACopeLan May 08 '17 at 00:27