简体   繁体   English

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

[英]Get height and width of TableLayoutPanel cell in Windows Forms

Using a TableLayoutPanel in Windows Forms. 在Windows窗体中使用TableLayoutPanel。 I am using RowStyles and ColumnStyles with SizeType as AutoSize and Percent respectively. 我正在使用RowStyles和ColumnStyles,SizeType分别为AutoSize和Percent。 I need to find out the absolute height and width of a cell in which a particular control is placed. 我需要找出放置特定控件的单元格的绝对高度和宽度。

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

Above, I am getting height as 0. RowStyle is with SizeType as AutoSize. 上面,我的高度为0. RowStyle的SizeType为AutoSize。 Similarly, I am getting as 33.33. 同样,我得到了33.33。 ColumnStyle is set with SizeType as Percent and Size = 33.33. ColumnStyle设置为SizeType为Percent,Size = 33.33。

I need to get absolute size in pixels for the cell. 我需要获得单元格的绝对大小(以像素为单位)。

For some odd reason, Microsoft decided to hide those functions from intellisense. 出于某种奇怪的原因,微软决定将这些功能隐藏在智能感知中。

This should work as written: 这应该按照书面形式工作:

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

Microsoft chose to hide the GetColumnsWidths() method from Intellisense (using the attribute EditorBrowsableState.Never) likely because they never really finished implementing the GetColumnsWidths() method. Microsoft选择隐藏Intellisense中的GetColumnsWidths()方法(使用属性EditorBrowsableState.Never),因为它们从未真正完成GetColumnsWidths()方法的实现。 The GetColumnsWidths() method returns an array as long as there is no control in the TableLayoutPanel that has a ColumnSpan value greater than 1. Once that condition exists, you're out of luck and the TableLayoutPanel's GetColumnsWidths() method will return an empty array instead. 只要 TableLayoutPanel中没有ColumnSpan值大于1的控件,GetColumnsWidths()方法就会返回一个数组。一旦存在这种情况,你运气不好,TableLayoutPanel的GetColumnsWidths()方法将返回一个空数组代替。

An alternative is to use the TableLayoutPanel's ColumnStyles and RowStyles collections--which returns the width/height of each column/row, respectively when the column/row SizeType is Absolute. 另一种方法是使用TableLayoutPanel的ColumnStyles和RowStyles集合 - 当列/行SizeType为Absolute时,它们分别返回每列/行的宽度/高度。 When it's Percent, the return value is a percentage value; 当它是百分比时,返回值是百分比值; when it's AutoSize, the return value appears to be 0. You can map a percent value to a pixel measurement by taking the total width of the TableLayoutPanel and subtracting the total width of any absolute columns then applying a percent calculation to the remaining pixels if no AutoSize columns are used (same applies to rows). 当它是AutoSize时,返回值似乎为0.您可以通过获取TableLayoutPanel的总宽度并减去任何绝对列的总宽度,然后将百分比计算应用于剩余像素(如果否),将百分比值映射到像素测量使用AutoSize列(同样适用于行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM