Get height and width of TableLayoutPanel cell in Windows Forms

后端 未结 2 1829
深忆病人
深忆病人 2020-12-31 04:53

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 a

相关标签:
2条回答
  • 2020-12-31 05:23

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

    0 讨论(0)
  • 2020-12-31 05:24

    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];
    
    0 讨论(0)
提交回复
热议问题