面板上的自动滚动显示不正确的不必要的滚动条 [英] AutoScroll on Panel Shows Incorrect Unnecessary Scrollbars

查看:68
本文介绍了面板上的自动滚动显示不正确的不必要的滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有首选大小的 winforms 用户界面.如果其父窗体的大小低于面板的 PreferredSize,则会自动显示滚动条,因为 AutoScroll 属性设置为 true.如果其父窗体的大小增加,则面板会填充额外的空间,并且隐藏滚动条.很简单.

I have a winforms user-interface that has a preferred size. If the size of its parent form is below the panel's PreferredSize, then scrollbars are automatically displayed because the AutoScroll property is set to true. If the size of its parent form is increased, then the panel fills the additional space, and the scrollbars are hidden. Simple enough.

问题在于,即使表单大于 PreferredSize,减小表单的大小也会短暂显示滚动条,即使它们是不必要的.

The problem is that even when the form is larger than the PreferredSize, decreasing the size of the form will briefly show the scrollbars, even though they are unnecessary.

以下简单示例重现了该问题.随着表单变小,即使未满足首选大小限制,滚动条也会随机出现.(Button 仅用于说明问题,实际 UI 更复杂).

The following simple example reproduces the problem. As the form is made smaller, the scrollbars will randomly appear even though the preferred size limit hasn't been met. (A Button is used to illustrate the problem only, the actual UI is more complicated).

不能使用 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);
    }
}

推荐答案

没关系,如果使用 ClientSize 而不是 Size,并取消注释 AutoScroll 行,然后它解决了问题.我会把它留在这里给子孙后代.

Nevermind, if ClientSize is used instead of Size, and uncomment the AutoScroll line, then it solves the problem. I'll leave it here for posterity.

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

这篇关于面板上的自动滚动显示不正确的不必要的滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆