-->

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

2019-07-04 14:03发布

使用Windows窗体一个TableLayoutPanel。 我分别使用RowStyles和ColumnStyles用的SizeType为自动调整大小和百分比。 我需要找出其中特定控制放置细胞的绝对高度和宽度。

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

以上,我得到高度为0 RowStyle与作为的SizeType自动调整大小。 同样地,我越来越为33.33。 ColumnStyle被设定为作为的SizeType百分比和尺寸= 33.33。

我需要的细胞像素获得绝对规模。

Answer 1:

对于一些奇怪的原因,微软决定隐藏智能感知这些功能。

这应该工作作为书面:

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


Answer 2:

微软选择隐藏GetColumnsWidths从智能感知()方法(使用属性EditorBrowsableState.Never)可能是因为他们从来没有真正完成实施GetColumnsWidths()方法。 该GetColumnsWidths()方法,只要有在TableLayoutPanel中没有控制具有ColumnSpan值大于1,一旦条件存在,你的运气和TableLayoutPanel中的GetColumnsWidths返回数组()方法将返回一个空数组代替。

另一种方法是使用TableLayoutPanel中的ColumnStyles和RowStyles集合 - 这返回每个列/行的宽度/高度,分别在列/行的SizeType是绝对的。 当它的百分比,返回值是一个百分比值; 当它的自动调整大小,返回值似乎是0。可以通过取TableLayoutPanel中的总宽度并减去任何绝对列的总宽度然后施加一个百分比计算,以剩余的像素,如果映射的百分比值,以一个像素测量无自动调整大小列使用(同样适用于行)。



文章来源: Get height and width of TableLayoutPanel cell in Windows Forms