ScrollBar 值设置不正确
我正在尝试实现类似于 Excel 的“无限”滚动性的功能;用户可以滚动到文档的“底部”;但然后继续滚动(使用滚轮或滚动条上的向下箭头),并且会为它们生成更多空行。我的这个大部分工作正常(当使用鼠标滚轮时它工作得很好);但我在使用 SmallIncrement 功能时遇到了麻烦 - 也就是说;当用户单击滚动条上的向下箭头时,尽管它位于滚动条可滚动范围的底部,但它应该向下滚动scrollbar.SmallChange。
这是我的代码(在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被设置回其原始值加一。如果我按住向下箭头,它基本上可以正常工作。一旦释放按钮,它仍然会跳回一点。
知道是什么原因造成的,以及有什么解决方法吗?
干杯!
编辑:这是我的滚轮代码。它是如此相似,以至于令人困惑为什么它不起作用。它位于包含面板的 MouseWheel
事件处理程序中。
int desiredValue = scrollBar.Value - e.Delta;
scrollBar.MaximumValue = (Math.Max(normalBottom, desiredValue + scrollBar.LargeChange));
scrollBar.Value = Math.Max(0, desiredValue);
normalBottom
是一个记住滚动条“有限”结尾的变量 - 在 Excel 中,这可能是用户输入的最低数据,也可能是屏幕的高度;因此它通常会滚动到该值之上(不会变为负值)。
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.
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;
}
}
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?
Cheers!
Edit: 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
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
滚动条发生的情况如下:当用户与导致事件的滚动条交互并且调用事件处理程序时,属性值尚未更新,在事件处理程序返回后,属性由滚动条在内部设置,覆盖您设置的值并导致您提到的“跳回”效果。它如何记住必须设置的值?很简单:就在 e.NewValue 中。这正是您的解决方案,为了能够在滚动事件期间正确地更改此属性的最终值,只需写入 e.NewValue 如下:
我想链接这些可能与您相关的页面:
http://msdn.microsoft.com/en -us/library/system.windows.forms.scrollbar.maximum.aspx
注意备注(这就是为什么即使移动Maximun,你仍然只能提前1):
。 forms.scrollbar.scroll.aspx" rel="noreferrer">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
(获取或设置滚动条的新值。[集中强调])
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:
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
Note in remarks (This is why even moving the Maximun you still get it advance only 1):
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])