C#创建控件数组有哪些?

2024-11-28 11:21:46
推荐回答(1个)
回答1:

还要再加上一句
btn[0]=new Button();
这样就行了
public Form1()

{

InitializeComponent();

Button[] btn = new Button[3];

int Top= 0;

for (int i = 0; i < 3; i++)

{

btn[i] = new Button();

btn[i].Top = Top;

this.Controls.Add(btn[i]);

Top += 30;

}

}

不知道为什么要定义二维数组,对二维数组的用处不太了解。不过用二维也是可以的。代码:
public Form1()

{

InitializeComponent();

Button[,] btn = new Button[3,3];

int Top= 0;

for (int i = 0; i < 3; i++)

{

for (int j = 0; j < 3; j++)

{

btn[i,j] = new Button();

btn[i,j].Top = Top;

this.Controls.Add(btn[i,j]);

Top += 30;

}

}

}