C# 动态编程由64个按钮(8x8)组成的网格

C# 动态编程由64个按钮(8x8)组成的网格,c#,winforms,arrays,button,grid,C#,Winforms,Arrays,Button,Grid,我正试图创建一个国际象棋游戏,纯粹是为了学习C#和国际象棋。首先,我想通过代码而不是设计器创建一个8x8的按钮网格。这将节省我硬编码每个按钮单独 按钮数组似乎是一个很好的开始方式,但我不知道如何实现它。您可以创建一个“square”类: 并定义一个阵列以放置8x8正方形 public partial class Form1 : Form { Square[,] square = new Square[8, 8]; public Form1() { InitializeComponen

我正试图创建一个国际象棋游戏,纯粹是为了学习C#和国际象棋。首先,我想通过代码而不是设计器创建一个8x8的按钮网格。这将节省我硬编码每个按钮单独

按钮数组似乎是一个很好的开始方式,但我不知道如何实现它。

您可以创建一个“square”类:

并定义一个阵列以放置8x8正方形

public partial class Form1 : Form
{
 Square[,] square = new Square[8, 8];

 public Form1()
 {
  InitializeComponent();
  int i, j;

  for (i = 0; i < 8; i++)
  {
     for (j = 0; j < 8; j++)
     {
       this.square[i, j] = new Square();//Creating the chess object//
       this.square[i, j].BackColor = System.Drawing.SystemColors.ActiveCaption;
       this.square[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
       this.square[i, j].Location = new System.Drawing.Point(57 + i * 40, 109 + j * 40);
       this.square[i, j].Name = "chessBox1";
       this.square[i, j].Size = new System.Drawing.Size(40, 40);
       this.square[i, j].TabIndex = 2;
       this.square[i, j].TabStop = false;
       this.Controls.Add(this.square[i, j]);
     }
  }
 }
}
公共部分类表单1:表单
{
正方形[,]正方形=新的正方形[8,8];
公共表格1()
{
初始化组件();
int i,j;
对于(i=0;i<8;i++)
{
对于(j=0;j<8;j++)
{
this.square[i,j]=new square();//创建国际象棋对象//
this.square[i,j].BackColor=System.Drawing.SystemColors.ActiveCaption;
this.square[i,j].BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
此.square[i,j].位置=新系统.Drawing.Point(57+i*40109+j*40);
这个.square[i,j].Name=“chessBox1”;
this.square[i,j].Size=新系统.Drawing.Size(40,40);
这个.square[i,j].TabIndex=2;
this.square[i,j].TabStop=false;
this.Controls.Add(this.square[i,j]);
}
}
}
}
int按钮宽度=40;
int按钮高度=40;
整数距离=20;
int start_x=10;
int start_y=10;
对于(int x=0;x<8;x++)
{
对于(int y=0;y<8;y++)
{
按钮tmpButton=新按钮();
tmpButton.Top=开始x+(x*按钮高度+距离);
tmpButton.Left=开始按钮y+(y*按钮宽度+距离);
tmpButton.Width=按钮宽度;
TMP按钮高度=按钮高度;
tmpButton.Text=“X:+X.ToString()+”Y:+Y.ToString();
//可能的添加按钮单击事件等。。
this.Controls.Add(tmpButton);
}
}

可能您可以使用下面的代码来解决您的问题。这段代码是用C#编写的Windows窗体应用程序。和控制按钮

    for (int i = 0; i< 8; i++)    
{
    for (int j = 0; j < 8; j++)
      {
        Button BtnNew = new Button;
        BtnNew.Height = 80;
        BtnNew.Width = 80;
        BtnNew.Location = new Point(80*i, 80*j);
        this.Controls.Add(BtnNew);
       }
}
for(int i=0;i<8;i++)
{
对于(int j=0;j<8;j++)
{
按钮BtnNew=新按钮;
Btnew.高度=80;
Btnew.宽度=80;
新位置=新点(80*i,80*j);
this.Controls.Add(BtnNew);
}
}

只需使用visual studio 2010添加im,我将创建此suign winformsUpdate您的问题,而不是添加此类注释。虽然StephanE的回答更适合我现在想要实现的内容,但您的代码为其他方法提供了更多帮助,我当然会进一步研究,我非常感谢您的帮助。谢谢,谢谢。与上面的代码非常相似,我一定会在我的模型中尝试。
        int ButtonWidth = 40;
        int ButtonHeight = 40;
        int Distance = 20;
        int start_x = 10;
        int start_y = 10;

        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 8; y++)
            {
                Button tmpButton = new Button();
                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();
                // Possible add Buttonclick event etc..
                this.Controls.Add(tmpButton);
            }

        }
    for (int i = 0; i< 8; i++)    
{
    for (int j = 0; j < 8; j++)
      {
        Button BtnNew = new Button;
        BtnNew.Height = 80;
        BtnNew.Width = 80;
        BtnNew.Location = new Point(80*i, 80*j);
        this.Controls.Add(BtnNew);
       }
}