【问题标题】:Insert Row in Middle of Table Layout panel C# Windows在表格布局面板的中间插入行 C# Windows
【发布时间】:2017-12-13 15:57:27
【问题描述】:

我正在尝试在表格布局面板的中间添加一行。但是,我正在努力寻找一种方法来做到这一点。我尝试关注互联网上的一篇文章,但这似乎不起作用。你能帮我吗。

我在表格布局面板中有 3 列和 5 行。每行包含标签、文本框和空白标签。

我正在尝试在已创建的表格布局面板中的第二行之后添加一行。

var AddOnControl = ConfirmationTable.Controls.Find("Discount", true).First();
int childIndex = 1+ ConfirmationTable.Controls.GetChildIndex(AddOnControl);


Label lbl = new Label();
lbl.Name = key;
lbl.Text = key;
lbl.Font = new Font("Calibri", 10F, System.Drawing.FontStyle.Regular);
lbl.Size = new Size(lbl.Size.Width + 70, lbl.Size.Height);

ConfirmationTable.Controls.Add(lbl);

TextBox txt = new TextBox();
txt.Multiline = true;
txt.TextChanged += txt_TextChanged;
txt.Name = "txtPrice" + key;
txt.Text = value;
txt.BorderStyle = BorderStyle.None;

ConfirmationTable.Controls.Add(txt);

lbl = new Label();
lbl.Name = "blank";
ConfirmationTable.Controls.Add(lbl);

ConfirmationTable.Controls.SetChildIndex(lbl, childIndex);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 1);
ConfirmationTable.Controls.SetChildIndex(txt, childIndex + 2);

但上面的代码总是在表格底部添加一行。有什么建议吗?

【问题讨论】:

    标签: c#


    【解决方案1】:

    Controls.Add 方法的重载接受两个整数作为列/行位置的索引。

    像这样:

    tableLayoutPanel1.Controls.Add(lb3, 0, 1);
    

    我相信这足以以简单的方式解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      相关资源
      最近更新 更多