c# - 从 TableLayoutPanel 单元格获取高度和宽度

标签 c# .net winforms cell tablelayoutpanel

我需要找出放置按钮的单元格的高度和宽度。 RowStyles 和 ColumnStyles 都设置为 Percent,TableLayoutPanel 的 Dock 为 Fill,按钮的 Dock 也是如此。

TableLayoutPanelCellPosition pos = tableLayoutPanel1.GetCellPosition(button1);
int width = tableLayoutPanel1.GetColumnWidths()[pos.Column];
int height = tableLayoutPanel1.GetRowHeights()[pos.Row];

但它不起作用。我收到以下错误:

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in < my program name.exe >

Additional information: Index outside of bounds of array."

我认为问题是因为我的应用程序适用于 .NET 3.5(并且不能再更高)。
那么如何获取单元格的高度和宽度呢?

最佳答案

我认为问题在于 button1 不在 tableLayoutPanel1 中。 所以调用 tableLayoutPanel1.GetCellPosition(button1) 导致 (-1,-1) 和 int width = tableLayoutPanel1.GetColumnWidths()[-1]; 绝对会抛出 IndexOutOfRangeException

关于c# - 从 TableLayoutPanel 单元格获取高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28558496/

相关文章:

c# - 异步 MVC Controller 和 HttpTaskAsyncHandler

c# - 在局部 View 中使用反射

c# - 从 .Net 应用程序处理 MySql 数据库的正确方法是什么

c# - 组合来自不同 URL 的 2 个 JSON 源

c# - 在 C# 中,finally block 之前有一个 catch-all catch block 有什么用?

c# - 你调用的对象是空的

c# - 在 C# 中比较数组的最简单方法

c# - 如何以编程方式将内容添加到菜单条?

winforms - 我无法让报告查看器在 VS 2019 上工作

c# - 使用 Xamarin 构建适用于 Windows CE 5.2 的应用程序?