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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:27:06+00:00 2026-06-17T14:27:06+00:00

hey guys i got a question right now I have a 8*8 array of

  • 0

hey guys i got a question right now I have a 8*8 array of buttons. They can change color by clicking on them. I want to make a reset button so when you click on the reset button they will return to the backcolor black again. Button1 is the reset button. it is at the bottum of the code.

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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((50 * x) + 30, (50 * y) + 30, 40, 40);
                    btn[x, y].Click += new EventHandler(this.btnEvent_click);
                    Controls.Add(btn[x, y]);
                    btn[x, y].BackColor = Color.Black;
                }
            }

            this.FormClosing += new FormClosingEventHandler(this.SaveEventHandler);
            LoadFromFile();
        }

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

        void SaveEventHandler(object sender, EventArgs e)
        {
            SaveToFile();
        }

        private const string filePath = @"C:\testmap\test.txt";
        private void LoadFromFile()
        {
            if (!System.IO.File.Exists(filePath))
                return;

            byte[] data = System.IO.File.ReadAllBytes(filePath);
            if (data == null || data.Length != btn.GetLength(0) * btn.GetLength(1) * 2)
                return;

            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);

                    string value = ((char)data[2 * position]).ToString() + ((char)data[2 * position + 1]).ToString();
                    Color color;
                    switch (value)
                    {
                        case "01":
                            color = Color.Red;
                            break;
                        case "00":
                            color = Color.Black;
                            break;
                        case "10":
                            color = Color.Green;
                            break;
                        case "11":
                            color = Color.Yellow;
                            break;
                        default:
                            color = Color.Black;
                            break;
                    }

                    btn[x, y].BackColor = color;
                }
            }
        }

        private void SaveToFile()
        {
            Dictionary<Form1, int> d = new Dictionary<Form1, int>();

            byte[] data = new byte[btn.GetLength(0) * btn.GetLength(1) * 2];
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    int position = (y * btn.GetLength(0) + x);
                    string value;
                    switch (btn[x, y].BackColor.Name)
                    {
                        case "Red":
                            value = "01";
                            break;
                        case "Black":
                            value = "00";
                            break;
                        case "Green":
                            value = "10";
                            break;
                        case "Yellow":
                            value = "11";
                            break;
                        default:
                            value = "00";
                            break;
                    }
                    data[2 * position] = (byte)value[0];
                    data[2 * position + 1] = (byte)value[1];
                }
            }

            System.IO.File.WriteAllBytes(filePath, data);
        }



        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
  • 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-17T14:27:07+00:00Added an answer on June 17, 2026 at 2:27 pm

    Just like you initialized the buttons, you can set the backcolor back to black:

    private void button1_Click(object sender, EventArgs e)
    {
            for (int x = 0; x < btn.GetLength(0); x++)
            {
                for (int y = 0; y < btn.GetLength(1); y++)
                {
                    btn[x, y].BackColor = Color.Black;
                }
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys. I have got a JPanel which changes color when it is clicked
hey guys i have got this far with a chat system but now i
Hey guys got a quick question here, how do i set up TweetStream after
Hey guys, I've got what I imagine is a simple question, but for some
Hey guys I got a weird issue.. I have a datalist that's only loaded
Hey Guys, I've got the following float array... public static float camObjCoord[] = new
Hey guys can you help me with this. I've got this '/[^A-Za-z]/' but cannot
Hey guys, got a problem with a question. Question : Write a declaration for
Hey guys. This is a follow-on from this question : After getting the right
hey guys, i have read This post, so what i got is JSON is

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.