Making control docking and scrollbars play nicely

我的未来我决定 提交于 2020-01-04 02:05:10

问题


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?


回答1:


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;
    }



回答2:


Try this:

Outer panel:{AutoScroll=true, Dock=Fill}
Inner panel:{Dock=Top,Width=customwidth}


来源:https://stackoverflow.com/questions/8674003/making-control-docking-and-scrollbars-play-nicely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!