从 TableLayoutPanel 单元格获取高度和宽度

Get height and width from TableLayoutPanel cell

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