【问题标题】:AutoScroll on Panel Shows Incorrect Unnecessary Scrollbars面板上的自动滚动显示不正确的不必要滚动条
【发布时间】: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


    【解决方案1】:

    没关系,如果使用ClientSize 代替Size,并取消注释AutoScroll 行,那么问题就解决了。我会把它留在这里供后代使用。

        scrollPanel.SizeChanged += delegate {
            //Size s = scrollPanel.Size;
            Size s = scrollPanel.ClientSize;
            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);
        };
    

    【讨论】:

      猜你喜欢
      • 2014-11-08
      • 2018-06-19
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多