.net - WinForms:更改 TableLayoutPanel 中行数的正确方法

标签 .net winforms desktop-application

我正在尝试更改 TableLayoutPanel 中的行数以编程方式(有时需要四个,有时五个,很少有六个)。

不幸的是,更改行数不会保留 RowStyles集契约(Contract)步,因此您无法设置新添加行的高度。以下测试代码证明了这一事实:

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

}

第二个断言失败。

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

第二个断言失败。

那么设置 RowCount 的正确编程方式是什么? TableLayoutPanel 的属性?

最佳答案

此问题已于 2005 年报告给 Microsoft,他们承认这是一个错误,但他们“仍在此处评估我们的选项Microsoft 已决定不修复它(“关闭”)。

TableLayoutPanel Rows and RowStyles do not correspond.

描述

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.

评论

Microsoft 于 2005 年 10 月 24 日下午 6:07 发布

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.

Microsoft 于 2005 年 6 月 27 日上午 6:49 发布

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.

Microsoft 于 2005 年 6 月 27 日下午 5:55 发布

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.

Microsoft 于 2005 年 7 月 6 日下午 3:43 发布

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.

Microsoft 于 2005 年 10 月 24 日下午 6:07 发布

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

关于.net - WinForms:更改 TableLayoutPanel 中行数的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/239938/

相关文章:

asp.net - .NET Core 应用程序是否需要在目标计算机上安装 .NET 运行时?

c# - 为什么 c# 上的 paypal 开发人员说明在 github 上不同?

mysql - 定义用于使用分析的 Web 服务(桌面应用程序)

c# - 带有基于桌面的实时应用程序的 Signalr

C# 类中的无效 token 'int'

c# - .NET 通用集?

c# - 如何在WFA中将数据添加到BoxPlot?

winforms - Windows 窗体中的十进制文本框

mysql - 将本地Mysql数据库同步到在线Mysql数据库

WPF 桌面应用程序读/写文件权限?