代码之家  ›  专栏  ›  技术社区  ›  Ally

自定义滚动条滚动触发器自动滚动条

  •  1
  • Ally  · 技术社区  · 8 年前

    我一直在使用一个用户控件来显示控件列表。我最初有 AutoScroll 启用,然后选择不使用它。我选择不使用它,因为它突出,只是不'看起来'与控制主题我一直在使用。

    我在一个叫做 MetroFramework ,我选择使用 MetroScrollBar 垂直滚动条的滚动条控件。

    我已经完全禁用了autoscroll,然后我决定实现滚动条。我只是通过:

    scbMain.Scroll += (sender, e) => { VerticalScroll.Value = scbMain.Value; };
    

    (其中 scbMain 是我正在讨论的滚动条吗)

    这是可行的,但并不像预期的那样。我一滚动,就会从默认滚动条中获得疯狂的闪烁效果,如图所示 here 是的。较长的列表 the same effect, but more pronounced 是的。

    我试图隐藏现有的滚动条:

    VerticalScroll.Visible = false;
    HorizontalScroll.Visible = false;
    VerticalScroll.Enabled = false;
    HorizontalScroll.Enabled = false;
    

    这对解决我的问题没有任何影响。

    需要注意的是:我的滚动条停靠在右边,在 UserControl 是的。

    1 回复  |  直到 8 年前
        1
  •  1
  •   γηράσκω δ' αεί πολλά διδασκόμε    8 年前

    好啊。问题解决了。问题出在这行代码中:

    scbMain.Scroll += (sender, e) => { ----> /*(Here*/ VerticalScroll.Value = scbMain.Value; <---- };
    

    实际上,您正在设置用户控件的滚动值,基本上是告诉系统调用autoscroll属性来设置该值!

    正确的方法不是自动滚动用户控件,而是在面板内滚动容器。所以在用户控件中添加一个面板。您将滚动面板及其内部的所有控件(在本例中,我将添加按钮)。

    this.btnExample.Location = new System.Drawing.Point(62, 0);
    this.btnExample.Name = "btnExample";
    this.btnExample.Size = new System.Drawing.Size(75, 390);
    this.btnExample.TabIndex = 1;
    this.btnExample.Text = "Out of Bounds";
    this.btnExample.UseVisualStyleBackColor = true;
    
    this.panel1.Controls.Add(this.btnExample);
    this.panel1.Location = new System.Drawing.Point(0, 0);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(270, 391); //the width must fit inside your user control and the height was arbitrary
    this.panel1.TabIndex = 2;
    
    this.Controls.Add(this.panel1);
    this.Controls.Add(this.scbMain);
    this.Name = "CtrlScroll";
    this.Size = new System.Drawing.Size(474, 300);
    

    滚动:

    public CtrlScroll() {
            InitializeComponent();
    
            scbMain.Scroll += ( sender, e ) => {
                //Normally the if statement whouldn't be needed but the metro srollbar
                //has a weird behaviour when the scroll value becomes max
                if( scbMain.Value > panel1.Height - this.Height ) {
                    panel1.Top = -( panel1.Height - this.Height );
                }
                else {
                    panel1.Top = -scbMain.Value;
                };
            };
    
            int maxVertical = panel1.Height;
    
            // SmallChange is typically 1%.
            int smallChangeVertical = Math.Max( (int)( maxVertical / 100 ), 1 );
    
            // LargeChange is one page.
            int largeChangeVertical = this.Height;
    
            scbMain.Minimum = 0;
            scbMain.Maximum = maxVertical;
            scbMain.SmallChange = smallChangeVertical;
            scbMain.LargeChange = largeChangeVertical;
        }