title: Preventing moving of a control out of its container
body: <p>This question is related to another question of mine which can be found here <a href="https://stackoverflow.com/questions/11994530/moving-a-control-within-another-controls-visible-area">can be found here</a>. I wanted to move a <code>PictureBox</code> within its parent container which is a <code>TabPage</code> (If it does make any difference!) Using the code below the movement can be done:</p>
  
  <pre><code>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; 
  } 
  </code></pre>
  
  <p>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 <code>TabPage</code> which my picturebox is inside it. I tried to add some limits for movement by changing the <code>MouseMove</code> event like this, to atleast prevent it from going out of the <code>Left</code> and <code>Right</code> visible area of the tabpage:</p>
  
  <pre><code>void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) { 
    if (_mapPackageIsMoving) { 
     //Added condition
    if (pictureBoxPackageView.Left &gt;= 0 &amp;&amp; pictureBoxPackageView.Right &gt;= 0)
      pictureBoxPackageView.Location = new Point( 
                               pictureBoxPackageView.Left + (e.X - start.X),  
                               pictureBoxPackageView.Top + (e.Y - start.Y)); 
    } 
  } 
  </code></pre>
  
  <p>But the problem with the code above is whenever the picturebox hits the right or left side of the container and the <code>Left</code> or <code>Right</code> get equal to 0, I can not move the control anymore.</p>
  
  <p>Any helps/tips to achive limiting this movement inside the container for <code>Left, Right, Top and Bottom</code> of the picture box will be appriciated!</p>