Making control docking and scrollbars play nicely

2019-07-03 00:58发布

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?

2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-03 01:18

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;
    }
查看更多
三岁会撩人
3楼-- · 2019-07-03 01:40

Try this:

Outer panel:{AutoScroll=true, Dock=Fill}
Inner panel:{Dock=Top,Width=customwidth}
查看更多
登录 后发表回答