我需要找出放置按钮的单元格的高度和宽度。 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/