pictureBox1_MouseClick event: Arrays/Squares (Game of life)
<p>I am experimenting and learning C# by coding the game of LIFE. Currently I have a <code>pictureBox</code> and a function <code>drawGrid</code> that creates a grid overlay. In order to click in each cell inside the <code>pictureBox</code> I have implemented the property <code>pictureBox1_MouseClick</code> in which there is an if decision logic to know if a cell is selected or not. The issues I am running into comes when i click the squares quickly i get an Error: <code>System.IndexOutOfRangeException</code> which points to <code>fill_in[x, y] = !fill_in[x, y];</code>. </p>

<p>How can I increase the clicking accuracy of the <code>pictureBox1_MouseClick</code> event so I dont get that error? </p>

<p>Specific error:</p>

<p>`An unhandled exception of type 'System.IndexOutOfRangeException' occurred in life.exe</p>

<p>Additional information: Index was outside the bounds of the array.`</p>

<p><strong>Code</strong></p>

<pre><code>namespace life
{
    public partial class Form1 : Form
    {

        Graphics paper;
        bool[,] fill_in = new bool[450, 450];
    int cellSize = 10;


 private void drawGrid()
        {
            int numOfCells = 100;        
            Pen p = new Pen(Color.Blue);
            paper.Clear(Color.White);

            for (int i = 0; i &lt; numOfCells; i++)
            {   
                // Vertical Lines
                paper.DrawLine(p, i * cellSize, 0, i * cellSize, numOfCells * cellSize);
                // Horizontal Lines
                paper.DrawLine(p, 0, i * cellSize, numOfCells * cellSize, i * cellSize);
            }
        }


 private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            int x = cellSize * (e.X / cellSize);
            int y = cellSize * (e.Y / cellSize);

            // Reverse the value of fill_in[i, j] - if it was false, change to true,
            // and if true change to false
            fill_in[x, y] = !fill_in[x, y];

            if (fill_in[x, y])
            {
                // Fill grid square with the filled color
                paper.FillRectangle(Brushes.Red, x, y, 10, 10);
            }
            else
            {
                // Fill grid square with unfilled color 
                paper.FillRectangle(Brushes.White, x, y, 10, 10);          
            }

        }

     }
}
</code></pre>
