我在程序运行时在表单中动态添加标签和文本框。如何在滚动窗格中插入这些组件,以便无论我添加多少标签和文本框,它们都适合表单?
我不知道我是否清楚......但我想要的是能够添加尽可能多的组件,而不是在表单的有限大小中......有没有办法做到这一点?
这是我现在的代码:
public void generateFormDynamically()
{
textBoxes = new TextBox[noOfPlayers];
int xLabel = 95;
int yLabel = 215;
int xTextBox = 205;
int yTextBox = 215;
for (int i = 0; i < noOfPlayers; i++)
{
Label label = new Label();
label.Text = "Player " + (i + 1) + ":";
if (i == 0) label.Location = new Point(xLabel, yLabel);
else
{
yLabel += 55;
label.Location = new Point(xLabel, yLabel);
}
label.AutoSize = true;
label.BackColor = System.Drawing.Color.Transparent;
label.Font = new System.Drawing.Font("Segoe UI Semibold", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label.Name = "label" + (i + 2);
label.Size = new System.Drawing.Size(68, 20);
label.TabIndex = 6;
label.Visible = true;
label.Show();
this.Controls.Add(label);
TextBox textBox = new TextBox();
if (i == 0) textBox.Location = new Point(xTextBox, yTextBox);
else
{
yTextBox += 55;
textBox.Location = new Point(xTextBox, yTextBox);
}
textBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textBox.Name = "textBox" + (i + 1);
textBox.Size = new System.Drawing.Size(245, 20);
textBox.TabIndex = 1;
textBox.Text = "Player" + (i + 1);
textBox.Visible = true;
textBox.Show();
textBoxes[i] = textBox;
this.Controls.Add(textBox);
}
}
谢谢你的帮助 :)