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 8636533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:15:04+00:00 2026-06-12T10:15:04+00:00

I am trying to create a game that has a grid and the user

  • 0

I am trying to create a game that has a grid and the user can click the grid areas to toggle their state. I am using WinForms. I can find 2 ways to do this, both seem complicated:

  1. A table-layout-panel, and place a label or button in each area, and when the button is clicked, figure out (somehow) which column and row the click was in and act accordingly.
  2. An unbound GridView.

Both seem very complicated to handle something like this.

For example, think of a tic-tac-toe game. All I want in this case is a 3×3 grid, and to know which area (x,y) was clicked and draw something in this area.

  • 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-12T10:15:06+00:00Added an answer on June 12, 2026 at 10:15 am

    Let me show you something i have written that come closer to your needs, maybe:

        private Button button;
        private Dictionary<string, Image> images = new Dictionary<string, Image>();
    
        public Form1()
        {
            InitializeComponent();
            InitializeTableLayoutPanel();
            AssignClickEvent();
    
            InitializeDictionary();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    
        /// <summary>
        /// Create Buttons for all Cells in the TableLayouPanel
        /// </summary>
        private void InitializeTableLayoutPanel()
        {         
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
            {
                for (int j = 0; j < tableLayoutPanel.RowCount; j++)
                {
                    button = new Button();
                    button.Visible = true;
                    button.Dock = DockStyle.Fill;
    
                    tableLayoutPanel.Controls.Add(button, i, j);
                }
            }
        }
    
        /// <summary>
        /// Assign a Click event of all Buttons in the TableLayoutPanel
        /// </summary>
        private void AssignClickEvent()
        {
            foreach (Control c in tableLayoutPanel.Controls.OfType<Button>())
            { 
                c.Click += new EventHandler(OnClick);
            }
        }
    
        /// <summary>
        /// Handle the Click event
        /// </summary>
        /// <param name="sender">Button</param>
        /// <param name="e">Click</param>
        private void OnClick(object sender, EventArgs e)
        {
            Button button = sender as Button;
            button.Visible = false;
    
            int column = tableLayoutPanel.GetPositionFromControl(button).Column;
            int row = tableLayoutPanel.GetPositionFromControl(button).Row;
    
            InitializePictureBox(column, row);
        }
    
        /// <summary>
        /// Create the PictureBox 
        /// </summary>
        /// <param name="column">TableLayoutPanel Column</param>
        /// <param name="row">TableLayoutPanel Row</param>
        private void InitializePictureBox(int column, int row)
        {
            PictureBox box = new PictureBox();
            box.Dock = DockStyle.Fill;
    
            string key = string.Format("{0}{1}", column.ToString(), row.ToString());
    
            box.Image = GetImageFromDictionary(key);
    
            tableLayoutPanel.Controls.Add(box, column, row);           
        }
    
        /// <summary>
        /// Get an Image from the Dictionary by Key
        /// </summary>
        /// <param name="key">the calling cell by combined column and row</param>
        /// <returns>Image</returns>
        private Image GetImageFromDictionary(string key)
        {
            return images.Where(x => x.Key == key).Select(x => x.Value).Cast<Image>().SingleOrDefault();             
        }
    
        /// <summary>
        /// Add Bitmaps to the Dictionary
        /// </summary>
        private void InitializeDictionary()
        {
            string key = string.Empty;
            for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
            {
                for (int j = 0; j < tableLayoutPanel.RowCount; j++)
                {
                    key = string.Format("{0}", i.ToString() + j.ToString());
                    Image image = CreateBitmap();
                    images.Add(key, image);
                }
            }
        }
    
        /// <summary>
        /// Create Bitmaps for the Dictionary
        /// </summary>
        /// <returns>Bitmap</returns>
        private Bitmap CreateBitmap()
        {
            System.Drawing.Bitmap image = new Bitmap(button.Width, button.Height);
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    image.SetPixel(x, y, Color.Red);
                }
            }
            return image;
        }
    

    Before Click:

    Before clicking the Button

    After Click:

    After clicking the Button

    Note that i’ve create columns and rows by designer, but i think it’s nothing special to create them programmatically.

    What could be the next steps?

    1. Create various colored pictures
    2. Create columns and rows of the panel programmatically
    3. Build it more flexible by setup individual settings

    Hope this helps a little bit.

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

Sidebar

Related Questions

What I am trying to do is create a game that has an extreme
I am trying to create a game for users to play online using their
I'm using VS2010 express to create a game built with xna . I'm trying
I am trying to create a form that creates a game and game_players at
I'm trying to create a custom UIView that can be displayed at any point
I'm trying to create a game that involves the computer typing to you. I'm
I'm trying to create a small game server using Ruby on Rails, Mongo, with
I am trying to create a go-moku game using jquery,php, and mysql database. I
I'm using gamequery to create a game, however, the documentation has nothing about using
Essentially I am trying to create a game, where the player has to dodge

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.