TableLayoutPanel – doesn't autosize or autoscroll correctly

I’ve been doing some Windows forms programming lately – not really my thing, but needs must. I’ve got an application which needs to dynamically create a form at run time, and so I’m using a System.Windows.Forms.TableLayoutPanel.

All the controls contained by the TableLayoutPanel resize automatically, and the TableLayoutPanel automatically provides scrollbars. “Great!”, I thought, “This will deal with large forms nicely.” Wrong!

The problem is when form has too many fields – it becomes deeper than the display area of the TableLayoutPanel. This is fine – the TableLayoutPanel should automatically add a vertical scrollbar, and adjust the size of the controls it contains to fit in the smaller area. Except it doesn’t. What I actually get is this:

Problems with TableLayoutPanel

Notice that I’ve got a vertical scrollbar, but it hasn’t resized the child components. This, they overlap some of the fields – and I now have a nice horizontal scrollbar. Arse.

I eventually found that others had had this problem and told Microsoft about it. However, they’ve decided not to fix it. Thanks Microsoft, that cost me an hour and half – it’d be wise to fix it. I found the answer from the same link – add a vertical scrollbar’s width as padding on the right of the TableLayoutPanel.

TableLayoutPanel – doesn't autosize or autoscroll correctly

8 thoughts on “TableLayoutPanel – doesn't autosize or autoscroll correctly

  1. Lisa Seacat says:

    I’m new to visual basic and have the same problem you’re describing. What do you mean by adding a vertical scrollbar’s width padding? I’m not sure how to do that. I can get the verticalscrollbar from my element but i can’t set the width.

  2. Sorry, typo there – it should read “add a vertical scrollbar’s width _AS_ padding on the right of the TableLayoutPanel.”

    Padding is applied to the inside of the TableLayoutPanel, and it forces the contents away from its border (or edge if you prefer). You can set different paddings for the top, bottom, left and right. By setting a right padding equal to the width of a vertical scrollbar, you’re making space for the scrollbar (should it appear – or you could force it to always be there) so that it doesn’t cover any of your controls.

    I don’t have the code on me right now, but it should be something like:

    Padding p = new Padding (0,0,myPanel.VerticalScroll.Width,0);
    myPanel.Padding = p;

    Hope that helps!

  3. Ah, I found my code – my example above was wrong:

    Padding p = myPanel.Padding;
    myPanel.Padding = new Padding( p.Left, p.Top, SystemInformation.VerticalScrollBarWidth p.Right, p.Bottom);

  4. wally says:

    F**K!! I wasted half of a f**king day because of this!!! I tried everything I could only come up with. Disabling scrollabrs, making them invisible, resizing the controls inside the table to make the whole thing narrower… everything! And the only thing you need to do is increase the table’s right padding! D; Stupid Microsoft!

    Thanks for sharing your findings. Finally I’ve got it fixed. Unfortunately, the controls are narrower than they could be because of the padding, but at least I don’t see the goddamn horizontal scrollbar anymore!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.