值我帶着同樣的疑問,對我能來到這個問題目前貢獻了:
int altura = this.Size.Height;
int largura = this.Size.Width;
int alturaOffset = 10;
int larguraOffset = 10;
int larguraBotao = 100; //button widht
int alturaBotao = 40; //button height
for (int i = 0; i < 50; ++i)
{
if ((larguraOffset+larguraBotao) >= largura)
{
larguraOffset = 10;
alturaOffset = alturaOffset + alturaBotao;
var button = new Button();
button.Size = new Size(larguraBotao, alturaBotao);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click;//function
button.Location = new Point(larguraOffset, alturaOffset);
Controls.Add(button);
larguraOffset = larguraOffset + (larguraBotao);
}
else
{
var button = new Button();
button.Size = new Size(larguraBotao, alturaBotao);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click;//function
button.Location = new Point(larguraOffset, alturaOffset);
Controls.Add(button);
larguraOffset = larguraOffset+(larguraBotao);
}
}
預期的行爲是,這將使用窗口大小的當前狀態,總是打破了線生成按鈕時,下一個按鈕會超出正確的MARGI n你的窗戶。
您必須指定高度和寬度,否則它們將不可見 – 2011-12-22 18:36:31
並且您再次得到有效答案,但您尚未接受它或解釋爲什麼它不合適... – Adam 2011-12-22 19:25:11