我正在尝试将带有文本框的行动态添加到“表布局面板”,并且我在此处的代码添加了该行,但是间距不正确。当添加行时,它比之前的行要小。关于如何添加行格式也与前面相同的任何想法?谢谢。
int A = 1;
public TextBox AddNewTextBox()
{
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = A * 28;
txt.Left = 15;
txt.Text = " ";
A = A + 1;
return txt;
}
public void DeleteTextBox()
{
if (A >= 2)
{
tableLayoutPanel1.Controls.RemoveAt(A);
A--;
}
}
private void AddRowBTN_Click(object sender, EventArgs e)
{
this.tableLayoutPanel1.RowCount++;
this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 28.57143F));
tableLayoutPanel1.Controls.Add(AddNewTextBox());
if(A >= 10)
{
this.tableLayoutPanel1.Size = new Size((282),(207*2)) ;
}
}
private void button8_Click(object sender, EventArgs e)
{
DeleteTextBox();
}
答案 0 :(得分:1)
将tableLayoutPanel的 AutoSize 属性设置为 true ,或在窗体Load或窗体的构造函数内部使用以下代码
tableLayoutPanel1.AutoSize = true;