问题描述
我有一个面板,有时需要比自然容纳的更多的垂直屏幕空间,因此它需要能够垂直滚动.因此,所有这些都设置为AutoScroll.
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.
控件包含在TableLayoutPanel中,并设置为停靠,因此它们应调整其宽度大小以匹配.但是,当控件触发滚动条时,它总是 最终创建一个水平滚动条,即使该控件没有最小宽度限制也是如此.它是根据先前的宽度创建水平滚动条,而不是遵循停靠命令并重新绘制控件以适应新的宽度.
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?
推荐答案
是的,这是布局计算方式的必然结果.摆脱水平滚动条将需要多次计算,但是.NET只进行一次.有一个很好的理由,布局可以是双稳态的,可以在两个状态之间不断地来回翻转.
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.
我真的不了解TableLayoutPanel在这里有什么用处或如何使它增长.通常,只是不要停靠它,而是给它提供要填充面板的尺寸.可能是这样的:
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;
}
这篇关于使控件停靠和滚动条很好地播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!