使控件停靠和滚动条很好地发挥作用

发布于 2024-12-24 02:07:54 字数 235 浏览 1 评论 0原文

我有一个面板,有时需要比自然适合的更多的垂直屏幕空间,因此它需要能够垂直滚动。所以,一切都设置为自动滚动。

这些控件包含在 TableLayoutPanel 中并设置为停靠,因此它们应该调整其宽度以匹配。然而,当控件触发滚动条时,它总是最终会创建一个水平滚动条,即使所违反的控件没有最小宽度限制。它根据先前的宽度创建水平滚动条,而不是尊重停靠命令并重新绘制控件以适应新的宽度。

有更好的方法解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音 2024-12-31 02:07:54

试试这个:

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

Try this:

Outer panel:{AutoScroll=true, Dock=Fill}
Inner panel:{Dock=Top,Width=customwidth}
孤独岁月 2024-12-31 02:07:54

是的,这是布局计算方式不可避免的结果。摆脱水平滚动条需要多次计算,但 .NET 仅进行一次计算。出于充分的理由,布局可以是双稳态的,在两种状态之间无休止地来回翻转。

我真的不明白 TableLayoutPanel 在这里有何用处,也不明白是什么让它增长。一般来说,只是不要停靠它,给它你想要填充面板的大小。也许是这样的:

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

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文