Get height and width of TableLayoutPanel cell in Windows Forms

Brij picture Brij · Dec 5, 2012 · Viewed 19.1k times · Source

Using a TableLayoutPanel in Windows Forms. I am using RowStyles and ColumnStyles with SizeType as AutoSize and Percent respectively. 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. Similarly, I am getting as 33.33. ColumnStyle is set with SizeType as Percent and Size = 33.33.

I need to get absolute size in pixels for the cell.

Answer

LarsTech picture LarsTech · Dec 5, 2012

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];