如何新建一个控件,用代码时间 c# winform问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 17:02:18
比如说自动新建4x4的button按钮??
新建的控件如何控制在form表中的初始位置?因为是4x4,所以要控制位置。

Button b1 = new Button();
Button b2 = new Button();
Button b3 = new Button();
Button b4 = new Button();

this.Controls.Add(b1);
this.Controls.Add(b2);
this.Controls.Add(b3);
this.Controls.Add(b4);

b1.Text = "1";
b2.Text = "2";
b3.Text = "3";
b4.Text = "4";

b1.SetBounds(1,1,100,100);
b2.SetBounds(101, 1, 100, 100);
b3.SetBounds(1, 101, 100, 100);
b4.SetBounds(101, 101, 100, 100);

楼上的说的就对.不过告诉你这些都的放在load事件中.

可以写成方法在Load事件里面调不就OK了