Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过“创建数组”;扫描“;另一个数组c#_C#_Arrays - Fatal编程技术网

通过“创建数组”;扫描“;另一个数组c#

通过“创建数组”;扫描“;另一个数组c#,c#,arrays,C#,Arrays,所以,我想做的是,制作一个按钮数组,但是只有按钮出现在先前确定的数组中。我不太擅长解释,所以我会用视觉表达我的意思 我知道它看起来很简单,我在5分钟内就把它拼好了,但我希望它能让我明白我想说的。我只是不知道怎么做。 正方形应该代表按钮。我试图在Visual Studio中从中创建他的 这就是我目前所得到的: private void Form1_Load(object sender, EventArgs e) { Marble(); } publ

所以,我想做的是,制作一个按钮数组,但是只有按钮出现在先前确定的数组中。我不太擅长解释,所以我会用视觉表达我的意思

我知道它看起来很简单,我在5分钟内就把它拼好了,但我希望它能让我明白我想说的。我只是不知道怎么做。 正方形应该代表按钮。我试图在Visual Studio中从中创建他的

这就是我目前所得到的:

    private void Form1_Load(object sender, EventArgs e)
    {
        Marble();

    }
    public void Marble()
    {
        int ButtonWidth = 40;
        int ButtonHeight = 40;
        int Distance = 20;
        int start_x = 10;
        int start_y = 10;
        int y = 0;
        int x = 0;
        int delX = x + (y * 2);

        for (x = 0; x < 8; x++)
        {

            for (y = 0; y < 8; y++)
            {


                GameButton tmpButton = new GameButton();
                tmpButton.BackColor = Color.Black;
                tmpButton.Top = start_x + (x * ButtonHeight + Distance);
                tmpButton.Left = start_y + (y * ButtonWidth + Distance);
                tmpButton.Width = ButtonWidth;
                tmpButton.Height = ButtonHeight;
                tmpButton.Text = "X: " + x.ToString() + " Y: " + y.ToString();
                tmpButton.MouseUp += TmpButton_MouseUp;
                tmpButton.Row = x;
                tmpButton.Column = y;
                tmpButton.Currentcolor = false;

                if (x == 4 && y == 6) {
                    tmpButton.BackColor = Color.White;


                }


                else
                {


                    this.Controls.Add(tmpButton);
                }

            }

        }

    }


    private void TmpButton_MouseUp(object sender, MouseEventArgs e)
    {
        GameButton Mygamebutton = (GameButton) sender;
        Mygamebutton.Currentcolor = !Mygamebutton.Currentcolor;
      if (Mygamebutton.Currentcolor == true)
        {
            Mygamebutton.BackColor = Color.Black;
        }
      else
        {
            Mygamebutton.BackColor = Color.White;
        }
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{
大理石();
}
公共空间大理石()
{
int按钮宽度=40;
int按钮高度=40;
整数距离=20;
int start_x=10;
int start_y=10;
int y=0;
int x=0;
int delX=x+(y*2);
对于(x=0;x<8;x++)
{
对于(y=0;y<8;y++)
{
GameButton tmpButton=新建GameButton();
tmpButton.BackColor=Color.Black;
tmpButton.Top=开始x+(x*按钮高度+距离);
tmpButton.Left=开始按钮y+(y*按钮宽度+距离);
tmpButton.Width=按钮宽度;
TMP按钮高度=按钮高度;
tmpButton.Text=“X:+X.ToString()+”Y:+Y.ToString();
tmpButton.MouseUp+=tmpButton_MouseUp;
tmpButton.Row=x;
tmpButton.Column=y;
tmpButton.Currentcolor=false;
如果(x==4&&y==6){
tmpButton.BackColor=Color.White;
}
其他的
{
this.Controls.Add(tmpButton);
}
}
}
}
私有void TmpButton_MouseUp(对象发送器,MouseEventArgs e)
{
GameButton Mygamebutton=(GameButton)发送方;
Mygamebutton.Currentcolor=!Mygamebutton.Currentcolor;
如果(Mygamebutton.Currentcolor==true)
{
Mygamebutton.BackColor=Color.Black;
}
其他的
{
Mygamebutton.BackColor=Color.White;
}
}
}

我将使用与阵列大小相同的
TableLayoutPanel
控件来满足此要求。 必须使用
for
(或嵌套for)结构来循环所有数组位置,如果在该位置内有所需的值,则在
TableLayoutPanel
的相同位置内创建一个按钮


如果您不知道
TableLayoutPanel
控件:

请首先创建所有按钮:

const int BUTTON_SIZE = 40;
int W = ParentPanel.Width / BUTTON_SIZE;
int H = ParentPanel.Height / BUTTON_SIZE;

for (int x = 0; x < W; x++ )
{
    for(int y = 0; y < H; y++)
    {
        Button btn = new Button();
        btn.Name = "button_" + x.ToString() + "_" + y.ToString();
        btn.Size = new Size(BUTTON_SIZE, BUTTON_SIZE);
        btn.Location = new Point(BUTTON_SIZE * x, BUTTON_SIZE * y);
        ParentPanel.Controls.Add(btn);
    }
}

这些按钮应该出现在网页、windows窗体应用程序、WPF应用程序或其他应用程序上?纯代码编写请求与堆栈溢出无关--我们希望这里的问题与特定编程问题有关--但我们很乐意帮助您自己编写!告诉我们,你被困在哪里了。这也将帮助我们更好地回答您的问题。数组大小是否总是相同的?我计划添加一个配置文件,您可以在其中更改代码中的不同设置,而不必深入研究所有内容。在该配置文件中,您可以更改布局和阵列的大小。对于此分配,我们不允许使用工具箱。。。这就是为什么我试图找到一种不同的方法来创建数组。要添加TableLayoutPanel,您不需要工具箱。您可以通过编程方式添加它
for(int x = 0; x < W; x++)
{
    for(int y = 0; y < H; y++)
    {
        Button btn = ParentPanel.Controls.OfType<Button>().FirstOrDefault(ctrl => ctrl.Name == "button_" + x.ToString() + "_" + y.ToString());
        if(btn != null)
        {
            // depending on if arr[x][y] == 1 make your action
        }
    }
}