简体   繁体   English

TableLayoutPanel只显示最后一行 - C#

[英]TableLayoutPanel only shows last row - C#

I've a TableLayoutPanel that has 10 rows and 2 columns. 我有一个有10行2列的TableLayoutPanel。

My problem is, when I add a label in the first column and a button in the other, everything is fine. 我的问题是,当我在第一列中添加标签而在另一列中添加一个按钮时,一切都很好。 But when I add another label and a button on eg the next row, the previous rows are empty. 但是当我在例如下一行添加另一个标签和按钮时,前面的行是空的。 It seems that my program only shows to last added row. 似乎我的程序只显示最后添加的行。

Code: 码:

Label lblProjectName = new Label();
lblProjectName.Text = "test";
lblProjectName.Anchor = AnchorStyles.Left;
Button btnProject = new Button();
btnProject.Text = "Fill In";

tlpProject.Controls.Add(lblProjectName, 0, 0);
tlpProject.Controls.Add(btnProject, 1, 0);

tlpProject.Controls.Add(lblProjectName, 0, 1);
tlpProject.Controls.Add(btnProject, 1, 1);

Tnx! TNX!

You have to create a new control for each cell in the panel. 您必须为面板中的每个单元格创建一个控件。

Currently, your code is moving the controls to the last column and last row. 目前,您的代码正在控件移动到最后一列和最后一行。

Something like this: 像这样的东西:

for (int i = 0; i < numRows; ++i) {
  Button btnProject = new Button();
  btnProject.Text = "Fill In";
  tlpProject.Controls.Add(btnProject, 1, i);
}

Any event handlers would have to be added here too, where on the handler end, you have to check the "sender" of the object to see which button the user pressed. 此处也必须添加任何事件处理程序,在处理程序端,您必须检查对象的“发送者”以查看用户按下的按钮。

It seems like Label and Button can only be children to single control while you are adding them into different cells(thus different controls). 当你将它们添加到不同的单元格(因此不同的控件)时,看起来Label和Button只能是单个控件的子控件。 If you need all cells filled with Labels and Buttons you will need 10 Labels and 10 Buttons for each column. 如果您需要所有填充标签和按钮的单元格,则每列需要10个标签和10个按钮。 Look at the code: 看看代码:

for(int i=0;i<10;i++){
var lblProjectName = new Lable();
lblProjectName.Text = "test";
lblProjectName.Anchor = AnchorStyles.Left;
var btnProject = new Button();
btnProject.Text = "Fill In";

tlpProject.Controls.Add(lblProjectName, 0, i);
tlpProject.Controls.Add(btnProject, 1, i);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM