获取Windows窗体中TableLayoutPanel单元格的高度和宽度

在Windows窗体中使用TableLayoutPanel。 我使用RowStyles和ColumnStyles分别使用SizeType作为AutoSize和Percent。 我需要找出放置特定控件的单元格的绝对高度和宽度。

TableLayoutPanelCellPosition pos = tableLayoutPanel1.GetCellPosition(button1); int height = (int)tableLayoutPanel1.RowStyles[pos.Row].Height; int width = (int)tableLayoutPanel1.ColumnStyles[pos.Column].Width; 

上面,我的高度为0. RowStyle的SizeType为AutoSize。 同样,我得到了33.33。 ColumnStyle设置为SizeType为Percent,Size = 33.33。

我需要获得单元格的绝对大小(以像素为单位)。

出于某种奇怪的原因,微软决定将这些function隐藏在智能感知中。

这应该按照书面forms工作:

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

Microsoft选择隐藏Intellisense中的GetColumnsWidths()方法(使用属性EditorBrowsableState.Never),因为它们从未真正完成GetColumnsWidths()方法的实现。 只要 TableLayoutPanel中没有ColumnSpan值大于1的控件,GetColumnsWidths()方法就会返回一个数组。一旦存在这种情况,你运气不好,TableLayoutPanel的GetColumnsWidths()方法将返回一个空数组代替。

另一种方法是使用TableLayoutPanel的ColumnStyles和RowStyles集合 – 当列/行SizeType为Absolute时,它们分别返回每列/行的宽度/高度。 当它是百分比时,返回值是百分比值; 当它是AutoSize时,返回值似乎为0.您可以通过获取TableLayoutPanel的总宽度并减去任何绝对列的总宽度,然后将百分比计算应用于剩余像素(如果否),将百分比值映射到像素测量使用AutoSize列(同样适用于行)。