views:

58

answers:

3

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?

A: 

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 a Panel:

listBox1.Width = panel2.Width - System.Windows.Forms.SystemInformation.VerticalScrollBarWidth;

HTH

adrift
+1  A: 

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.

Hans Passant
Well, I was looking at xnview for what I wanted to do, and xnview has the behaviour done very well. But I suspect they didn't use the autoscroll functionality. But autoscroll is just so darned handy, too bad it doesn't quite work right. Too bad Microsoft couldn't get it right. disappointing that Microsoft didn't.
Matthias Wandel
Not sure what that means. But yes, this won't go wrong when auto-scroll is not supported by a library. If only windows had their scrollbars *outside* of the window, it would have never been a problem. I nevertheless really liked them trying to make it work.
Hans Passant
A: 

I just encountered this issue. The fix I used was to set Scrollable to false and then to true. Here is an example with a ListView Resize event:

private void myListView_Resize(object sender, EventArgs e)
{
 this.SuspendLayout();

 //Code to do various resizing stuff

 //Force Scrollbar Recalculation
 myListView.Scrollable = false;
 myListView.Scrollable = true;
 this.ResumeLayout(false);
 this.PerformLayout();
}

If Scrollable is not always true, you can make the recalculation conditional.

Brian