Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making control docking and scrollbars play nicely

I've got a panel which will sometimes need more vertical screen space than naturally fits, so it needs to be able to vertically scroll. So, it's all set to AutoScroll.

The controls are contained within a TableLayoutPanel and set to dock, so they should resize their width to match. Yet, when the control triggers the scrollbar, it always ends up creating a horizontal scrollbar, even though there's no minimum width constraint on the control that's being violated. It's creating the horizontal scrollbar based on the previous width rather than respecting the dock command and redrawing the control to fit the new width.

Is there a better way round this?

like image 997
eftpotrm Avatar asked Oct 19 '25 13:10

eftpotrm


2 Answers

Try this:

Outer panel:{AutoScroll=true, Dock=Fill}
Inner panel:{Dock=Top,Width=customwidth}
like image 164
Reza ArabQaeni Avatar answered Oct 22 '25 03:10

Reza ArabQaeni


Yes, that's an inevitable consequence from the way layout is calculated. Getting rid of the horizontal scrollbar would require multiple passes through the calculation but .NET only makes one pass. For a good reason, layout can be bi-stable, flipping back and forth between two states endlessly.

I don't really understand how a TableLayoutPanel would be useful here or what makes it grow. In general, just don't dock it, give it the size you want to fill the panel. Something like this perhaps:

    bool resizingTlp;

    private void tableLayoutPanel1_Resize(object sender, EventArgs e) {
        if (resizingTlp) return;
        resizingTlp = true;
        if (tableLayoutPanel1.Height <= panel1.ClientSize.Height) tableLayoutPanel1.Width  panel1.ClientSize.Width;
        else tableLayoutPanel1.Width = panel1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth;
        resizingTlp = false;
    }
like image 24
Hans Passant Avatar answered Oct 22 '25 03:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!