I'm writing an app in .net that uses the autoscroll for a layout panel in a dialog. It seems that whenever I resize the window so that the vertical scrollbars should appear, the horizontal scrollbar automatically appears also. Looking closely at it, the second scrollbar now allows me to scroll the window by 16 pixels (the width of the other scrollbar). So windows seems to think I need a client area that is at least as wide as it was before the vertical scrollbar appeared.
If I now resize the window to be 16 pixels wider (so that my window area is as wide as it was before the scrollbar appeared), the scrollbar goes away. Now if I resize it back down to what it was, it stays away.
So it would appear to me that there is a bug in the system where the minimum width is somehow sticky, but upsizing and downsizging the window (with the mouse, and without tuching the scrollbars related APIs) clears the condition
Does anybody know of a workaround, or am I doing something to trip up Windows?
I just encountered this issue. The fix I used was to set
Scrollable
tofalse
and then totrue
. Here is an example with aListView
Resize
event:If
Scrollable
is not always true, you can make the recalculation conditional.This is a known bug in windows - here
Best way to fix this is to put the table layout panel autosized inside another panel which is docked to the main form and set with autoscroll = true
So you are no longer using the tablelayoutpanel to scroll which is buggy, you use the panel to scroll and the tablelayoutpanel is inside the panel
I haven't noticed exactly the behavior you describe, but have run into situations where the appearance of the vertical scrollbar makes a horizontal scrollbar necessary.
You could set the contents of the panel to allow for the width of the scrollbar, for example if I have a
ListBox
in aPanel
:HTH
Yes, I think you already diagnosed the problem correctly. It is a nasty side effect of, say, the vertical scrollbar appearing and needing space, making the available client area smaller. Too small to fit the controls, now also making the horizontal scrollbar appear. It is actually bi-stable, the horizontal bar can flicker on and off in certain cases.
To avoid this effect, the layout engine would have to make multiple passes through the layout, dealing with the changing client area. It however only makes one pass. Which sounds wise, this could be a potentially never ending loop. I don't know of a decent fix for this. Your user will probably just resize the window large enough to get rid of at least one of the scrollbars.
Despite this being an old question, it is still a problem in .NET 4. Having read as much as I could find on the issue, I have rolled a combination of solutions into a helper class.
First, here is the result that I am shooting for ... I have a panel that contains a variety of controls. The child controls, and their sizes, can change based on user activity. I want the panel to resize horizontally so that there is never a horizontal scroll bar, but if there is not enough room vertically, I want the vertical scroll bar to appear. Further, the vertical scroll bar can not cover any of my child controls when it appears, and I don't want to leave a gap for it when it is not needed.
The two 'bugs' that my helper class attempts to fix are first, never show the horizontal scroll bar, and second, when the vertical scroll bar appears, have the panel's width increase automatically to accommodate it.
My assumptions are that the panel is set to AutoSize and AutoScroll, and the child controls are set to AutoSize as well.
The Solution
The helper class attaches itself to the panel (by handling the Paint and SizeChanged events) and does two things. First, it disables the horizontal scroll bar. This is not as easy as it sounds, and I found the solution to this problem here Horizontal scroll bar answer by Kbv Subrahmanyam. Second, in response to the Paint and SizeChanged events, as well as a background timer, it checks to see if the Visible property of the vertical scroll bar has changed. If so, the helper class alters the Right Padding property of the panel to add or remove the extra space the scroll bar requires. The use of the various panel events and the timer are required because .NET exposes no events at all for the scroll bar (a big design flaw IMHO).
Once final point is that you can't do anything that changes the size of the panel while handling the SizeChanged event. Bad Stuff(tm) happens if you do. So, if I need to change the padding due to a SizeChanged event, I schedule that change for later.
Anyway, here is the code for the helper class. It assumes you have all the appropriate 'using' statements, including one for System.Threading ...