Get height and width of TableLayoutPanel cell in Windows Forms

假装没事ソ 提交于 2019-11-30 08:24:14
LarsTech

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. 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.

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. 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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!