您可以 - 只是将TextBox的高度添加到表单的大小,但是tbh最好使用指定TextBoxes大小的常量,然后添加该值。
要移动listBox/button2,将它们锚定到表单底部,并且它们将自动保持与表单底部相同的距离。
至于动态生成,使用一个List(或一个堆栈,取决于你在做什么)。
partial class Form1 : Form
{
List<TextBox> textBoxes = new List<TextBox>(); // or stack
const int textBoxWidth = 200; // control variables for TextBox placement
const int textBoxHeight = 50;
const int textBoxMargin = 5;
void button1_Click(object sender, EventArgs e)
{
this.Height += textBoxHeight + textBoxMargin;
TextBox tb = new TextBox();
if (textBoxes.Count == 0)
{
tb.Top = textBoxMargin;
}
else
{
tb.Top = ((textBoxHeight + textBoxMargin) * textBoxes.Count) + textBoxMargin;
}
tb.Left = textBoxMargin;
tb.Height = textBoxHeight;
tb.Width = textBoxWidth;
textBoxes.Add(tb);
this.Controls.Add(tb);
}
}
这应该工作。这里的方法与几乎所有的位置自定义都可以使用常量值完成。
在WinForms中最好使用它吗?那么,在WinForms中没有真正的理由不这样做,这个功能很容易实现。我自己是WPF的人,但这仍然是合法的。
编辑的逻辑错误
这是好的,做到这一点。 –
您是否尝试过使用FlowLayoutPanel? – sallushan