title: C# runtime error: index out of range
body: <p>I have to make a simple windows forms application in which the player takes turns with the computer in taking sticks (which I made out of labels which go invisible), but I am having a problem with the code for the pc as it causes a runtime error "index out of range", and I can't figure it out..</p>
  
  <pre><code>    private void pcTake(int x)
      {
          textBox1.Text = "I take " + x;
          for (int i = 0; i &lt; labels.Count; i++)
          {
              if (labels[i].Visible == false &amp;&amp; labels[i + 1].Visible == true)
              {
                  while (x &gt; 0)
                  {
                      if (x + i &gt; labels.Count)
                          break;
                      labels[i + x].Visible = false;
                      x--;
                  }
                  break;
              }
          }
  
      }
  </code></pre>
  
  <p><em>x</em> is a random number,
  <em>labels</em> is a list containing the labels</p>
  
  <pre><code>foreach (Control c in this.Controls)
          {
              if (c is Label)
              {
                  labels.Add(c);
                  c.Enabled = true;
              }
          }
  </code></pre>
  
  <p>thanks in advance</p>