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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:45:04+00:00 2026-06-16T20:45:04+00:00

I am trying to make a array of 8×8 buttons, and so far it

  • 0

I am trying to make a array of 8×8 buttons, and so far it works.
Now I have stumbled upon a problem.
I want the color of the button (backcolor) to change when it is clicked. And change to a different color when clicked again.

This is my code so far:

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        Button[,] btn = new Button[8,8];

        public Form1()
        {
            InitializeComponent();

            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    btn[x,y] = new Button();
                    btn[x,y].SetBounds(40 * x, 40 * y, 40, 40);
                    btn[x,y].Click += new EventHandler(this.btnEvent_click);
                    Controls.Add(btn[x, y]);
                    btn[x,y].BackColor = Color.Black;
                }
            }

            /* 
            btn.Click += new EventHandler(this.btnEvent_click);
            btn[x,y].Text = Convert.ToString(x+","+y);
            Controls.Add(btn);
            btn[x,y].Click += new EventHandler(this.btnEvent_click);
            */
        }

        private void form1_load(object sender, EventArgs e)
        {

        }

        void btnEvent_click(object sender, EventArgs e)
        {
           (Control)sender).BackColor = Color.Red;
        }
    }
}

So far I can only change the color to red, and I’ve tried multiple if and for statements to change the color a second time.

Could anyone help me out?

  • 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-16T20:45:05+00:00Added an answer on June 16, 2026 at 8:45 pm

    Hi Temporary you can use below solution:

    void btnEvent_click(object sender, EventArgs e)
    {
        Control ctrl = ((Control)sender);
        switch (ctrl.BackColor.Name)
        {
            case "Red":
                ctrl.BackColor = Color.Yellow;
                break;
            case "Black":
                ctrl.BackColor = Color.White;
                break;
            case "White":
                ctrl.BackColor = Color.Red;
                break;
            case "Yellow":
                ctrl.BackColor = Color.Purple;
                break;
            default:
                ctrl.BackColor = Color.Red;
                break;
        }
    }
    

    I know there can be a better solution also, but meanwhile you can go with this…you can add more colors also in switch statment as required

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

Sidebar

Related Questions

I have a flat array that I'm trying to make multidimensional. Basically, I want
I have an array of raw sound data samples, and I'm trying to make
I'm trying to make array of strings, I have function rLine which reads line
I am trying to make an array of objects. I have Two classes, the
I'm trying to make an array from file path components. I have an array
I was trying to make an array-based linear list, then I compiled this: char
I am trying to make an array of strings and then pluck a random
I'm trying to make a dynamic array in C# but I get an annoying
I'm writing some error checking and trying to make use of an boolean array
I'm trying to subclass Array in ruby to make it randomize its elements when

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.