如果你需要更新你的,试试List<Button>
这个int [][]
var nums = new[] { new[] { 0, 1, 2, 3 }, new[] { 4, 5, 6, 7 }, new[] { 8, 9, 10, 11 }, new[] { 12, 13, 14, 15 } };
int counter = 0;
foreach (int[] ints in nums)
{
foreach (int i in ints)
{
buttonList[counter].Text = i.ToString();
counter++;
}
}
解决此问题的更好方法是:
private List<Button> buttonList = new List<Button>();
private void addButtonsDynamically(object sender, EventArgs e)
{
int top = 10, left = 10;
for (int i = 1; i <= 16; i++)
{
Button btn = new Button();
btn.Parent = this;
btn.Size = new Size(25, 25);
btn.Text = (i - 1).ToString();
btn.Location = new Point(left, top);
left += 35;
if (i > 0 && i % 4 == 0)
{
top += 35;
left = 10;
}
}
}
它将创建如下输出: