Wednesday, August 10, 2016

Windows Forms:TableLayoutPanel's Hidden Methods GetColumnWidths/GetRowHeights

The TableLayoutPanel Windows Forms control allows a grid of controls to be displayed. This is a handy control for organizing uniform layouts but when using TableLayoutPanel it is not clear how to determine the widths of each column and the height of each row. The TableLayoutPanel  control contains the GetColumnWidths and GetRowHeights that provide, respectively array of column widths and row heights for the table. These methods are as follows:
  [Browsable(false)]
  [EditorBrowsable(EditorBrowsableState.Never)]
  public int[] GetRowHeights();

  [Browsable(false)]
  [EditorBrowsable(EditorBrowsableState.Never)]
  public int[] GetColumnWidths();

As was demonstrated in "C#: EditorBrowsabilityAttribute and hiding Methods, Properties and Field from IntelliSense," the EditorBrowsability attribute hides a method from IntelliSense when the attribute is constructed by passing in a value of EditorBrowsableState.Never. This means that GetColumnWidths and GetRowHeights are hidden from developers which is odd because the documentation does not say these methods are deprecated (what reason does Microsoft have for hiding these rather useful methods?). Here we see that the GetRowHeights method is excluded from the methods displayed by IntelliSense:


 Even though the GetRowHeights method is not visible it completely valid to use the method in the code. The previous code snippet is part of the code behind of the following Windows Form:



The previous screenshot shows a Windows Forms TableLayoutPanel control containing five rows and three columns: The left column contains a Label in each row of the column and the middle column contains a TextBox in each row of the column. The right column contains no controls.

By clicking on the NumericUpDown control labeled, "Number of Rows to Show", the number of visible rows associated with the TableLayoutPanel control can be changed by summing the heights of the visible rows and assigning the height of the TableLayoutPanel to be equal to the height of all visible rows. The code for handling this as follows, specifically the SetVisibleRows method:


The previous code only works because each row within the TableLayoutPanel  control is set to an absolute pixel width. If the rows were set to be 20% each then changing the table height would not hide rows within the table.

An example of the application displaying two visible rows is as follows:


GetRowHeights was shown to be extremely handy but for some unknown reason Microsoft has choosen to hide this method GetRowHeights and its counterpart, GetColumnsWidths, from developers.

No comments :

Post a Comment