我正在尝试将带有文本框的行动态添加到“表布局面板”,并且我在此处的代码添加了该行,但是间距不正确。当添加行时,它比之前的行要小。关于如何添加行格式也与前面相同的任何想法?谢谢。

    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();
    }

最佳答案

将tableLayoutPanel的AutoSize属性设置为true或在窗体Load上或窗体的构造函数内使用以下代码

tableLayoutPanel1.AutoSize = true;

关于c# - 动态将行添加到表布局面板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51793271/

10-11 04:39