Reputation: 256761
i'm trying to change the number of rows in a TableLayoutPanel programatically (sometimes it needs to be four, sometimes five, and rarely six).
Unfortunatly changing the number of rows does not keep the RowStyles
collection in sync, so you are then not able to set the height of the newly added rows. The following test code demonstrates this fact:
private void button1_Click(object sender, EventArgs e)
{
//TableLayoutPanels start with 2 rows by default.
Debug.Assert(tableLayoutPanel1.RowStyles.Count ==
tableLayoutPanel1.RowCount);
//Cannot remove rows
tableLayoutPanel1.RowCount = 1;
Debug.Assert(tableLayoutPanel1.RowStyles.Count ==
tableLayoutPanel1.RowCount);
}
The second assertion fails.
private void button2_Click(object sender, EventArgs e)
{
//TableLayoutPanels start with 2 rows by default.
Debug.Assert(tableLayoutPanel1.RowStyles.Count ==
tableLayoutPanel1.RowCount);
//Cannot add rows
tableLayoutPanel1.RowCount = 6;
Debug.Assert(tableLayoutPanel1.RowStyles.Count ==
tableLayoutPanel1.RowCount);
}
The second assertion fails.
So what's the proper programatic way to set the RowCount
property of a TableLayoutPanel
?
Upvotes: 2
Views: 4784
Reputation: 256761
This issue was reported to Microsoft in 2005, and they acknowledge it's a bug, but they were "still evaluating our options here" Microsoft has decided not to fix it ("Closed").
TableLayoutPanel Rows and RowStyles do not correspond.
Description
When you create a tableLayoutPanel, and new rows are created (either by adding rows in the program or setting the RowCount property), new RowStyles are not added to the control. Additionally, if I add new Row Styles, the number of rows is increased, but not to the same number: if I start with two Rows and two RowStyles, set Rowcount to 4, and then add two RowStyles (in design view), the Rowcount has set to 6. It seems more intuitive for the ordinality of the RowStyles collection to match the current RowCount.
Posted by Microsoft on 10/24/2005 at 6:07 PM
This issue has been reactivated as we begin planning for the next version of Visual Studio. Over the coming months we will reconsider feedback that was previously postponed. We welcome your comments and participation in this process.
Posted by Microsoft on 6/27/2005 at 6:49 AM
The Microsoft Sub-status is now "Reproduced"
Thanks for reporting this bug, we have been able to repro this issue and are investigating.
Thank you, Prabhu, VS2005 Product Team.
Posted by Microsoft on 6/27/2005 at 5:55 PM
Thankyou for reporting this issue. This is a peculiarity in our runtime object model that doesn't translate well to design time. At runtime the RowCount and ColCount really mean minRowCount and minColCount in terms of autogrow, because rows/cols don't need a supporting style. At design time we try to simplify that and keep a near 1:1 correspondence between styles and rows/cols. In this case, you are seeing by design runtime behavior. We already have a bug tracking this issue and are still evaluating our options here. Thanks again for contributing to a better Whidbey.
Posted by Microsoft on 7/6/2005 at 3:43 PM
Thankyou for reporting this issue. This is a peculiarity in our runtime object model that we have chosen to not address at design time. At runtime the RowCount and ColCount really mean minRowCount and minColCount in terms of autogrow, because rows/cols don't require a supporting style. At design time we generally try to simplify that and keep a near 1:1 correspondence between styles and rows/cols. In the case of rowSpan or colSpan on a table layot panel with autogrow you can get into a state where rows/rowcount/rowstyles are out of sync. To avoid this, simply add the columns/rows you need first, then set the controls *span property. Thanks.
Posted by Microsoft on 10/24/2005 at 6:07 PM
This issue has been reactivated as we begin planning for the next version of Visual Studio. Over the coming months we will reconsider feedback that was previously postponed. We welcome your comments and participation in this process.
-- Visual Studio Team
Upvotes: 7
Reputation: 49482
Have you tried creating a new RowStyle
and then adding it using the tableLayoutPanel1.RowStyles.Add
method?
Upvotes: 0