ScrollBars On TableLayoutPanel

The TableLayoutPanel control can be very useful for creating Forms, but it also can cause some problems, here I will try to describe the most common. Let me start with the simple form:

Simple form with tableLayoutPanel

The advantage of using the TableLayoutPanel is automatic alignment of all textBoxes and fit their positions to the longest label. Everything looks good and works fine as long as you do not want to add scrollbar to your TableLayoutPanel. When you do this, you will see always both vertical and horizontal scrollbars.

Simple form with tableLayoutPanel

The Reason:

It seems that the TableLayoutPanel has bug in mechanism for estimating actual width and it always thinks the control is too narrow to accommodate its contents. Microsoft has know about this problem but they won't fix it, here is a bug page:

The Solution:

I have spent some time till I have found the solution. To solve this issue we need to add a right padding. This padding should be so wide as scrollbar. The width of scrollbar we can get from: System.Windows.Forms.SystemInformation class. This class has two static properties: HorizontalScrollBarHeight and VerticalScrollBarWidth. We have to use second property but in most cases both of them has the same value.

Here is a all code:

    tblLayout.Padding = new Padding(0, 0, System.Windows.Forms.SystemInformation.VerticalScrollBarWidth, 0);

The final Result:

Simple form with tableLayoutPanel

The main disadvantage of this solution is fact that all text fields became a little narrower, but this is the only one solution which works in this case.