二维数组按钮?

时间:2012-10-28 15:59:42

标签: c# winforms

我想在Windows窗体中创建二维按钮数组。我必须使用for循环,因为我想拥有一个包含100个按钮的数组。但是我无法让它发挥作用。我尝试使用List>和Buttton [,]但是不起作用。当我这样做时:

private List<List<Button>> LEDarray = new List<List<Button>>();

        for (int y = 0; y < 5; y++)
        {
            this.tempList.Clear();

            for (int x = 0; x < 20; x++)
            {
                this.tempList.Add(new Button());
            }

            this.LEDarray.Add(tempList);
        }
        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 20; x++)
            {
                this.LEDarray[y][x].BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
                this.LEDarray[y][x].FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                this.LEDarray[y][x].ForeColor = System.Drawing.Color.Black;

                xPos = xOffset + LEDsize * x + 20;
                yPos = yOffset + LEDsize * y + 20;

                this.LEDarray[y][x].Location = new System.Drawing.Point(xPos, yPos);
                this.LEDarray[y][x].Name = "button" + y.ToString() + x.ToString(); 
                this.LEDarray[y][x].Size = new System.Drawing.Size(LEDsize, LEDsize);
                this.LEDarray[y][x].TabIndex = 0;
                this.LEDarray[y][x].UseVisualStyleBackColor = false;
            }
        }

        for (int y = 0; y < 5; y++)
        {
            for (int x = 0; x < 20; x++)
            {
                this.Controls.Add(this.LEDarray[y][x]);
            }
        }

它只显示最后一行按钮。所以只是第五行而不是前一行。当我尝试与Button数组类似时,它根本不起作用。但阵列方式只是SOS调用。我想用List做到这一点你能帮助我使用上面的代码使它工作吗?

2 个答案:

答案 0 :(得分:4)

向控件添加按钮时,将条件for (int y = 0; y < 0; y++)更改为for (int y = 0; y < 5; y++)

第二个问题是你正在清除你刚刚添加到LEDarray的tempList,引用。为每行按钮创建新列表:

for (int y = 0; y < 5; y++)
{
     // tempList.Clear() - this will remove all buttons from previous row
     tempList = new List<Button>();

     for (int x = 0; x < 20; x++)     
          tempList.Add(new Button());     

     LEDarray.Add(tempList);
}

另外,我建议您为按钮增加TabIndex

this.LEDarray[y][x].TabIndex = 10 * y + x;

另一个建议 - 创建自定义LEDButton类并使用它:

public class LEDButton : Button
{
    public const int LEDWidth = 20;
    public const int LEDHeight = 20;

    public LEDButton()
    {
        BackColor = Color.FromArgb(0, 64, 0);
        ForeColor = Color.Black;
        FlatStyle = FlatStyle.Flat;
        Size = new Size(LEDWidth, LEDHeight);
        UseVisualStyleBackColor = false;
    }
}

用法:

// initialize array
LEDButton[,] leds = new LEDButton[5, 20];
for (int y = 0; y < leds.GetUpperBound(0); y++)
    for (int x = 0; x < leds.GetUpperBound(1); x++)
        leds[y, x] = new LEDButton()
            {
                Name = String.Format("Button{0}{1}", y, x),
                TabIndex = 10 * y + x,
                Location = new Point(LEDButton.LEDWidth * x + 20,
                                     LEDButton.LEDHeight * y + 20)
            };
// add buttons to controls
for (int y = 0; y < leds.GetUpperBound(0); y++)
    for (int x = 0; x < leds.GetUpperBound(1); x++)
         Controls.Add(leds[y, x]);

答案 1 :(得分:0)

由于您似乎混淆了边界值,请尝试使用 foreach : - )

foreach (var row in this.LEDArray)
{
    foreach (var button in row)
    {
        this.Controls.Add(button);
    }
}

可以说,这传达了代码的意图 - 添加集合中的所有按钮 - 更好。