Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:34:09+00:00 2026-06-12T15:34:09+00:00

I am experimenting and learning C# by coding the game of LIFE. Currently I

  • 0

I am experimenting and learning C# by coding the game of LIFE. Currently I have a pictureBox and a function drawGrid that creates a grid overlay. In order to click in each cell inside the pictureBox I have implemented the property pictureBox1_MouseClick 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: System.IndexOutOfRangeException which points to fill_in[x, y] = !fill_in[x, y];.

How can I increase the clicking accuracy of the pictureBox1_MouseClick event so I dont get that error?

Specific error:

`An unhandled exception of type ‘System.IndexOutOfRangeException’ occurred in life.exe

Additional information: Index was outside the bounds of the array.`

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 < 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);          
            }

        }

     }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:34:10+00:00Added an answer on June 12, 2026 at 3:34 pm

    Please make sure your calculations return desired values:

    int x = cellSize * (e.X / cellSize);
    int y = cellSize * (e.Y / cellSize);
    

    In this scenario x and y can be outside the boundaries of your array. Example: cellsize * (450 / cellsize) equals 450 and this causes the error since allowed indices range from 0 to 449.

    EDIT:

    To fix this problem (and this is a really dirty and temporary fix), please change your code as follows:

    int x = cellSize * (e.X / cellSize);
    int y = cellSize * (e.Y / cellSize);
    
    x = x >= 450 ? 450 - 1 : x;
    y = y >= 450 ? 450 - 1 : y;
    

    More desirable and actually a lot smarter solution would be simply to adjust your table’s size to be exactly the same as the range of e.X and e.Y.

    Hope this helps,
    Piotr

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently learning Fortran 90 and have been experimenting with the kind and
I have been learning ASP.NET by experimenting, watching videos on ASP.NET, and reading forums.
Just experimenting and learning, and I know how to create a shared dictionary that
i've started learning about javascript closures, and while experimenting, i realised that the following
I am currently learning and experimenting with C and am using Bloodshed's DEV-C++ as
I've been learning and experimenting with Java Generics for a while but I have
I'm learning Google App Engine and experimenting with an application that does a lot
I'm still learning JavaScript,reading books,utilizing FireBug,experimenting. I'm amazed and stuck on thing below. Have
I'm learning Prolog, and as an exercise, I'm experimenting with a simple database that
I am going through a learning process by coding in C# the Game of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.