title: How do you make an object be limited to the boundaries of a Form
body: <p>I am currently making a Space Invaders type game using C# Windows Forms. When creating the laser cannon which the user controls, I want them to move left and right, shooting lasers.  This is my current code for the movement so far:</p>
  
  <pre><code>enter code here
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  
  namespace Move
  {
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void button1_KeyDown(object sender, KeyEventArgs e)
      {
  
      }
  
      private void pictureBox1_Click(object sender, EventArgs e)
      {
  
      }
  
      private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
          int i;
  
          for (i = 0; i &lt; 500; i++)
          {
  
              if (e.KeyCode == Keys.Up)
              {
                  pictureBox1.Location = new Point(pictureBox1.Left - 1);
                  Application.DoEvents();
                  System.Threading.Thread.Sleep(10);
              }
  
              if (e.KeyCode == Keys.Down)
              {
                  pictureBox1.Location = new Point(pictureBox1.Left + 1);
                  Application.DoEvents();
                  System.Threading.Thread.Sleep(10);
              }
  
  
  
  
          }
        }
  
      private void Form1_Load(object sender, EventArgs e)
      {
  
      }
  }
  }
  </code></pre>
  
  <p>However, I'm having an issue with this; if the image goes too far, it goes out of the boundaries of the form but I want it to do is bounce off and go the other way. </p>
  
  <p>I tried doing this but to no avail, it only moves by a pixel but I think it's a good step(right?...):</p>
  
  <pre><code>if(pictureBox1.Location == new Point(300,300))
              {
                  pictureBox1.Location = new Point(pictureBox1.Left - 1);
              }
  </code></pre>
  
  <p>Like how do I get the correct X,Y coordinates of the whole form so I could tell it to go the other way as well as actually getting it to move? </p>