滚动条值设置不当 [英] ScrollBar value set incorrectly

查看:392
本文介绍了滚动条值设置不当的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现类似于Excel的无限滚动性的东西;在用户可以滚动到文档的底部;但随后继续滚动(使用滚轮或滚动条的向下箭头),和更多的,是对他们产生的空行。我有这样的大多是工作(使用鼠标滚轮它完美的时候);但我有麻烦与SmallIncrement功能 - 那就是,当用户点击滚动条上的向下箭头,它应该下降scrollbar.SmallChange,尽管是在滚动条的滚动范围的底部。

I am trying to implement something similar to Excel's "infinite" scrollability; in that a user can scroll to the "bottom" of the document; but then keep scrolling (using either the scrollwheel or the down arrow on the scrollbar), and more, empty rows are generated for them. I have this mostly working (when using the mouse scrollwheel it works perfectly); but I am having troubles with the SmallIncrement functionality - that is; when the user clicks the down arrow on the scrollbar, it should go down by scrollbar.SmallChange, despite being at the bottom of the scrollbar's scrollable range.

下面是我的code(在scrollBar_Scroll的处理程序):

Here is my code (in the handler of scrollBar_Scroll):

int difference = e.NewValue - e.OldValue;
if (e.Type == ScrollEventType.SmallIncrement)
{
    if (difference != scrollBar.SmallChange)
    {
        int increase = (scrollBar.SmallChange - difference);
        scrollBar.Maximum += increase;
        scrollBar.Value += increase;
    }
}

看着它在调试器,这台价值完全按照我期望的那样。然而,有些东西(不知道)发生在函数结束后,造成scrollBar.Value被设置回其原始值,加一。如果我按住向下箭头,它的工作原理主要是正确的。它仍然跳回了一下,一旦松开按钮。

Looking at it in the debugger, this sets the values exactly as I would expect. However, something (not sure what) happens after the function ends, causing scrollBar.Value to be set back to its original value, plus one. If I hold down the down-arrow, it works mostly correctly. It still jumps back up a bit once the button is released.

任何想法,这可能是造成这一点,任何方式固定呢?

Any idea what could be causing this, and any way of fixing it?

干杯!

编辑:这是我的滚轮code。它是如此的相似,这令人困惑,为什么它不工作。这是在包含面板的鼠标滚轮事件处理程序。

Here is my scrollwheel code. It's so similar that it's confusing why it's not working. This is in the containing panel's MouseWheel event handler.

int desiredValue = scrollBar.Value - e.Delta;
scrollBar.MaximumValue = (Math.Max(normalBottom, desiredValue + scrollBar.LargeChange));
scrollBar.Value = Math.Max(0, desiredValue);

normalBottom 是一个变量记住了有限结尾的滚动条 - 在Excel中,这将是无论是最低的用户输入的数据,或者屏幕的高度;所以通常滚动高于此值(无需负)。

normalBottom is a variable remembering the "finite" ending of the scrollbar - in excel, this would be either the lowest user-entered data, or the height of the screen; so it scrolls normally above this value (without going negative).

推荐答案

这是怎么回事你的滚动条是这样的:当用户使用滚动条交互,导致事件和事件处理程序被称为属性值还没有被更新后,你的事件处理程序返回的属性内部设置滚动条覆盖您设置的值,造成跳背的效果你提到。它是如何记住它有什么价值设置?很简单:它在e.NewValue。而这也正是您的解决方案,才能够正确的方式来改变滚动事件时此属性的终值只写e.NewValue如下:

What is happening with your scrollbar is the following: when the user interacts with the scrollbar causing the event and your event handler is called the property value have not yet been updated, after you event handler returns the property is set internally by the scrollbar overwriting the value you set and causing the "jump back" effect you mention. How does it remember what value it have to set? Easy: it's in e.NewValue. And that's exactly your solution, to be able to correctly way to alter the final value of this property during the scroll event just write to e.NewValue as follows:

int difference = e.NewValue - e.OldValue;
        if (e.Type == ScrollEventType.SmallIncrement)
        {
            if (difference != scrollBar.SmallChange)
            {
                int increase = (scrollBar.SmallChange - difference);
                scrollBar.Maximum += increase;
                e.NewValue = scrollBar.Value + increase;
            }
        }

我要链接theese页面可能与您: <一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.maximum.aspx">http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.maximum.aspx

I want to link theese pages that may be relevant to you: http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.maximum.aspx

请注意在备注(这就是为什么即使移动最高可你还是把它提前只有1人):

Note in remarks (This is why even moving the Maximun you still get it advance only 1):

最大值只能以编程方式达到。一个滚动条的值在运行时不能达到通过用户交互其最大值。可以通过用户交互来达到的最大值等于1加最大属性值减去LargeChange属性值。如果需要的话,可以将最大属性设置为对象的大小-1至占1的术语

The maximum value can only be reached programmatically. The value of a scroll bar cannot reach its maximum value through user interaction at run time. The maximum value that can be reached through user interaction is equal to 1 plus the Maximum property value minus the LargeChange property value. If necessary, you can set the Maximum property to the size of the object -1 to account for the term of 1.

<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.scroll.aspx">http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.scroll.aspx <一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.scrolleventargs.newvalue.aspx">http://msdn.microsoft.com/en-us/library/system.windows.forms.scrolleventargs.newvalue.aspx (获取或设置滚动条的新的价值。[重点成套])

http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollbar.scroll.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.scrolleventargs.newvalue.aspx (Gets or sets the new Value of the scroll bar. [Emphasis in sets])

这篇关于滚动条值设置不当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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