使用二维数组键c#数组

2023-09-03 07:02:53 作者:゛人不帅,气质在′

上面的图片是怎样的程序看起来

四大顶级按钮被放在一个二维数组如下:

 专用键[,] btns;

        公共Form1中()
        {
            的InitializeComponent();

            btns =新按钮[,] {{按钮2,按钮1},
                                   {将Button4,按钮3}};
        }
 

这四个按钮都被初始化为

 的foreach(在btns VAR BTN)
            {
                btn.Enabled = FALSE;
            }
 

,当你点击下方的两个按钮,每个按钮,在该行什么,我想要做的是 能够和彩色签署(红色和蓝色反过来就像一个连接4场比赛)

我已成功地解决了一半的问题,但是当我点击ROW1按钮,它不断让行2,以及当我点击ROW2按钮,它从一排启用

我怎么能限制每个按钮来处理二维数组,因此它不仅使正确的行。

这里是全code

 命名空间WindowsFormsApplication1
{
    公共部分类Form1中:形态
    {

        私人按钮[,] btns;

        公共Form1中()
        {
            的InitializeComponent();

            btns =新按钮[,] {{按钮2,按钮1},
                                   {将Button4,按钮3}};
        }

        私人无效Form1_Load的(对象发件人,EventArgs的)
        {
            的foreach(在btns VAR BTN)
            {
                btn.Enabled = FALSE;
            }
        }
        INT CC = 0;


        私人无效button5_Click(对象发件人,EventArgs的)
        {

            的foreach(在btns VAR BTN)
            {

                如果(!btn.Enabled)
                {
                    btn.Enabled = TRUE;

                    如果(CC == 0)
                    {
                        CC = 1;
                        btn.BackColor = Color.Red;

                    }
                    其他
                    {
                        CC = 0;
                        btn.BackColor = Color.Blue;
                    }


                    返回;
                }
            }
        }


        私人无效button6_Click(对象发件人,EventArgs的)
        {

            的foreach(在btns VAR BTN)
            {

                如果(!btn.Enabled)
                {
                    btn.Enabled = TRUE;

                    如果(CC == 0)
                    {
                        CC = 1;
                        btn.BackColor = Color.Red;

                    }
                    其他
                    {
                        CC = 0;
                        btn.BackColor = Color.Blue;
                    }

                    返回;
                }
            }
        }
    }
}
 
c语言中二维数组如何使用

解决

这个问题就解决了​​,这是我如何解决它:

 私人无效button5_Click(对象发件人,EventArgs的)
            {
               按钮[] ROW1 =新按钮[] {按钮2,按钮1};
                的foreach(在ROW1 VAR roww1)
                {

                    如果(!roww1.Enabled)
                    {
                        roww1.Enabled = TRUE;

                        如果(CC == 0)
                        {
                            CC = 1;
                            roww1.BackColor = Color.Red;

                        }
                        其他
                        {
                            CC = 0;
                            roww1.BackColor = Color.Blue;
                        }


                        返回;
                    }
                }
            }
 

解决方案

在使用多维数组,你需要使用一个for循环,而不是的foreach遍历数组的一行迭代的时间:

 私人无效button5_Click(对象发件人,EventArgs的)
{
   对于(INT COL = 0;山坳< btns.GetLength(1)+列)
   {
      VAR BTN = btns [0,列]。
   // SNIP
}

私人无效button6_Click(对象发件人,EventArgs的)
{
   对于(INT COL = 0;山坳< btns.GetLength(1)+列)
   {
      VAR BTN = btns [1,COL]。
   // SNIP
}
 

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

解决方案

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
}