更改滚动值时滚动条不会更新
在 Panel
中设置 VerticalScroll.Value
并将 AutoScroll
设置为 true
时,滚动位置会相应更改,但位置拇指没有。我是否需要以某种方式更新滚动条(面板上的 Invalidate(true)
不起作用)?有什么想法吗?
When setting VerticalScroll.Value
in a Panel
with AutoScroll
set to true
, the scroll position changes accordingly but the position of the thumb does not. Do I need to update the scrollbar somehow (Invalidate(true)
on the panel does not work)? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试调用
.PerformLayout();
Try calling
.PerformLayout();
感谢您的
.PerformLayout()
提示!在我的例子中这还不够,我在表单
Form.Shown
事件处理程序中设置 VerticalScroll.Value,并且出于某种原因我必须添加DoEvents
指令预先让滚动起作用。这是我的
Shown
事件处理程序:ScrollPanel 控件的类型为
System.Windows.Forms.Panel
。如果没有
Application.DoEvents()
行,垂直滚动值设置将被完全忽略。我想这可能对其他人有用。
Thanks for the
.PerformLayout()
tip!It wasn't enough in my case, I am setting the VerticalScroll.Value within the form
Form.Shown
event handler, and for some reason I had to add aDoEvents
instruction beforehand for the scrolling to work.Here's my
Shown
event handler:ScrollPanel control is of type
System.Windows.Forms.Panel
.Without the
Application.DoEvents()
line, the vertical scrolling value setting was completely ignored.I thought it might came in handy for someone else.
当尝试自动滚动包含图片框的面板时,我遇到了完全相同的问题。
我找到了另一种方法来使其发挥作用。将滚动值增加两次:
第一次增加值会导致图片框在面板内滚动,但 .Value 不会更改,滑块也不会移动。
第二次增加该值会导致 VerticalScroll.Value 增加,移动拇指,但图片框不会再次滚动。
对我来说似乎是一个错误。当然,图片框的滚动和 .Value 的递增以及拇指的移动都应该发生在第一行代码之后。
I had exactly the same issue when trying to autoscroll a panel containing a picture box.
I found another method to make it work. Increment the scroll value twice:
Incrementing the value the first time causes the picture box to scroll within the panel, but the .Value doesn't change and the thumb doesn't move.
Incrementing the value the second time causes the VerticalScroll.Value to be incremented, moving the thumb, but the picture box is not scrolled again.
Seems like a bug to me. Surely both the scrolling of the picture box and the incrementing of the .Value and movement of the thumb should happen after the first line of code.
我遇到了几乎相同的情况,其中我在带有滚动条的面板内有一个 PictureBox(显示图形),并且我想放大或缩小以保持鼠标指针位置“明显”静止。在面板内重新定位 PictureBox 会导致与滚动条的同步中断,此后,使用滚动条滚动将不会显示 PictureBox 的完整区域。解决方案是通过将计算值分配给 panel.HorizontalScroll.Value 和/或 panel.VerticalScroll.Value 属性来完成移动。但仅此还不够:需要使用这两种解决方案之一来使应用程序按设计运行:
上面 KMan 的解决方案:
或者上面 JJMcLellan 的解决方案:在我的例子中,将计算值分配两次,例如:
其中任何一个似乎都具有相同的最终结果。由于上面的 #2 使它看起来像一个错误,并且 #1 似乎(希望)是 Microsoft 设计的(?),所以我最终在我的应用程序中使用了 #1。我只是想确认这两种方法都可以在 VB.NET 环境中工作。
I ran into something nearly identical where I have a PictureBox (showing a graph) inside a panel with scroll bars, and I want to zoom in or out keeping the mouse-pointer position "apparently" stationary. Re-positioning the PictureBox inside the panel to do this throws off synchronization with the scroll bars, and thereafter, scrolling with the scroll bars will not show the full area of the PictureBox. The solution is to accomplish the move by assigning a computed value to the panel.HorizontalScroll.Value and/or panel.VerticalScroll.Value properties. But that alone isn't enough: one of these two solutions is required to make the application behave as designed:
KMan's solution above:
Or JJMcLellan's solution above: in my case, assigning the computed value twice, e.g.:
Either one of these appears to have the same end results. Since #2 above makes it look like a bug, and #1 seems (hopefully) to be by design by Microsoft (?), I ended up using #1 in my application. I just wanted to confirm both these work in a VB.NET environment.
我试图让它在表单上工作。我尝试了 PerformLayout(),我尝试了 Application.DoEvents(),我尝试分配 VerticalScroll.Value 两次,但都不起作用。我要做的唯一一件事就是将 VerticalScroll 的属性更改为不同的值,然后将其改回来:
请注意,更改 VerticalScroll.Value 也同样有效,但您必须实际修改其值,并确保最小值到最大值范围内的变化比暂时增加最大值要复杂得多。
I was trying to get it to work on a Form. I tried the PerformLayout(), I tried Application.DoEvents(), I tried assigning the VerticalScroll.Value twice, none of it worked. The only thing I got to work was to change a property of VerticalScroll to a different value, and then change it back:
Note that it works just as well to change VerticalScroll.Value, but you have to actually modify its value, and making sure the change is in the range of Minimum to Maximum is a lot more complex than just increasing Maximum temporarily.