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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:59:48+00:00 2026-06-13T16:59:48+00:00

the image above is how the program looks the four top buttons have been

  • 0

image

the image above is how the program looks

the four top buttons have been put in a 2d array as follows

private Button[,] btns;

        public Form1()
        {
            InitializeComponent();

            btns = new Button[,] { { button2 , button1 },
                                   { button4 , button3 }};
        }

the four buttons have been initialized to

            foreach (var btn in btns)
            {
                btn.Enabled = false;
            }

and what i want to do is when you click the bottom two buttons each button in that row
enables and a color is signed (red and blue in turn like a connect 4 game)

i have managed to solve half of the problem but when i click the row1 button it keeps enabling row 2 as well and when i click on row2 button it starts enabling from row one

how can i restrict each button to deal with the 2d array so it only enables the correct rows.

here is the full code

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        private Button[,] btns;

        public Form1()
        {
            InitializeComponent();

            btns = new Button[,] { { button2 , button1 },
                                   { button4 , button3 }};
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var btn in btns)
            {
                btn.Enabled = false;
            }
        }
        int cc = 0;


        private void button5_Click(object sender, EventArgs e)
        {

            foreach (var btn in btns)
            {

                if (!btn.Enabled)
                {
                    btn.Enabled = true;

                    if (cc == 0)
                    {
                        cc = 1;
                        btn.BackColor = Color.Red;

                    }
                    else
                    {
                        cc = 0;
                        btn.BackColor = Color.Blue;
                    }


                    return;
                }
            }
        }


        private void button6_Click(object sender, EventArgs e)
        {

            foreach (var btn in btns)
            {

                if (!btn.Enabled)
                {
                    btn.Enabled = true;

                    if (cc == 0)
                    {
                        cc = 1;
                        btn.BackColor = Color.Red;

                    }
                    else
                    {
                        cc = 0;
                        btn.BackColor = Color.Blue;
                    }

                    return;
                }
            }
        }
    }
}

SOLVED

This problem is solved and this is how i solved it:

 private void button5_Click(object sender, EventArgs e)
            {
               Button[] row1 = new Button[] {button2, button1};
                foreach (var roww1 in row1)
                {

                    if (!roww1.Enabled)
                    {
                        roww1.Enabled = true;

                        if (cc == 0)
                        {
                            cc = 1;
                            roww1.BackColor = Color.Red;

                        }
                        else
                        {
                            cc = 0;
                            roww1.BackColor = Color.Blue;
                        }


                        return;
                    }
                }
            }
  • 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-13T16:59:50+00:00Added an answer on June 13, 2026 at 4:59 pm

    When using a multidimensional array, you need to use a for loop instead of foreach to iterate through one row of the array at a time:

    private void button5_Click(object sender, EventArgs e)
    {
       for (int col = 0; col < btns.GetLength(1); ++col)
       {
          var btn = btns[0, col];
       //snip
    }
    
    private void button6_Click(object sender, EventArgs e)
    {
       for (int col = 0; col < btns.GetLength(1); ++col)
       {
          var btn = btns[1, col];
       //snip
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image above a div, and IE (7, and presumably 6) insists
what i am trying to achive is as follows: in the image above you
http://i44.tinypic.com/b69h1h.jpg Take a look at the above image, especially the Firebug window. Now have
In java...I have an integer array of RGB pixels, means output of my program
I have a program that calculates the convex hull of an image. I'm trying
I have a program that scans an image and converts gems to specific numbers.
I have a small project that a program used to analyze an image to
The program above is, as said, a Sudoku game. I have written down ideas
Image describing the problem http://img215.imageshack.us/img215/3725/20100314235127schedule2.jpg I hope the image above can explain what I
My question is I wanna apply strategy pattern to save log from Image above

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.