【发布时间】:2014-08-25 22:15:16
【问题描述】:
我有一个具有首选大小的 winforms 用户界面。如果其父窗体的大小低于面板的PreferredSize,则自动显示滚动条,因为AutoScroll 属性设置为true。如果增加其父窗体的大小,则面板会填充额外的空间,并且隐藏滚动条。很简单。
问题在于,即使表单大于PreferredSize,减小表单大小也会短暂显示滚动条,尽管它们是不必要的。
以下简单示例重现了该问题。随着表单变小,滚动条将随机出现,即使尚未满足首选大小限制。 (Button仅用于说明问题,实际UI更复杂)。
不能使用 WPF。
public class Form6 : Form {
Control panel = new Button { Text = "Button" };
public Form6() {
this.Size = new Size(700, 700);
Panel scrollPanel = new Panel();
scrollPanel.AutoScroll = true;
scrollPanel.Dock = DockStyle.Fill;
scrollPanel.SizeChanged += delegate {
Size s = scrollPanel.Size;
int minWidth = 400;
int minHeight = 400;
panel.Size = new Size(Math.Max(minWidth, s.Width), Math.Max(minHeight, s.Height));
// this is a little better, but still will show a scrollbar unnecessarily
// one side is less but the other side is >=.
//scrollPanel.AutoScroll = (s.Width < minWidth || s.Height < minHeight);
};
scrollPanel.Controls.Add(panel);
this.Controls.Add(scrollPanel);
}
}
【问题讨论】:
标签: c# winforms user-interface panel autoscroll