鼠标按下时自动滚动问题

发布于 2024-10-17 11:11:32 字数 207 浏览 5 评论 0原文

我在自动滚动面板内有一个自定义控件。当用户控件超出面板宽度时,会出现滚动条。然后,当您滚动任意距离并在控件上按下鼠标时,滚动条会弹回到 0。有人知道为什么会这样吗?我很确定我不会尝试在任何地方更改滚动条的值...

谢谢

编辑:这似乎只发生一次,第一次单击它时,每次它都会按预期工作

编辑2:它也当您打开一个新窗口,然后返回到 C# 窗口时会发生这种情况

I have a custom control inside an autoscroll panel. When the user control extends beyond the width of the panel, the scroll bars appear. When you then scroll any distance, and mousedown on the control, the scrollbar snaps back to 0. Does any one know why that might be? I'm pretty sure I am not trying to change the value of the scrollbar anywhere...

Thanks

EDIT: This only appears to happen once, the first time you click on it, every other time it works as expected

EDIT 2: It also happens when you bring a new window up, and then go back to the C# window

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不…忘初心 2024-10-24 11:11:33

我为 VB.net 解决了这个问题。要尝试此操作,请创建一个 WinForms 项目并:

  1. 在 Form1 上放置一个 Panel1,在 Panel1 内放置一个 TextBox1。
  2. 使 TextBox1 比 Panel1 大,并用一堆文本填充它。
  3. 将Panel1.AutoScroll 设置为true。
  4. 将 Button1 添加到 Form1 并将其 TabIndex 设置为 0 以获取加载焦点。

运行该项目,移动 Panel1 滚动条,然后单击 TextBox1 中的某些文本。当 Panel1 尝试将 TextBox 的左上角滚动到视图中时,TextBox1 会令人烦恼地跳转。现在将下面的代码放入 Form1 中并重复测试。好多了!这在 VB 2010 Express 中有效。

Delegate Sub AutoScrollPositionDelegate(ByVal sender As ScrollableControl, ByVal p As Point)
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter

   Dim p As Point = Panel1.AutoScrollPosition
   Dim del As AutoScrollPositionDelegate = New AutoScrollPositionDelegate(AddressOf SetAutoScrollPosition)

   Panel1.BeginInvoke(del, {Panel1, p})

End Sub
Private Sub SetAutoScrollPosition(ByVal sender As ScrollableControl, ByVal p As Point)

   p.X = Math.Abs(p.X)
   p.Y = Math.Abs(p.Y)
   sender.AutoScrollPosition = p

End Sub

I worked this out for VB.net. To try this create a WinForms project and:

  1. Place a Panel1 on Form1 and a TextBox1 inside Panel1.
  2. Make TextBox1 bigger than Panel1 and fill it with a bunch of text.
  3. Set Panel1.AutoScroll to true.
  4. Add a Button1 to Form1 and set it's TabIndex to 0 to grab the focus on loading.

Run the project, move the Panel1 scrollbars around and then click on some text in TextBox1. TextBox1 will jump annoyingly as Panel1 tries to scroll the TextBox's upper left corner into view. Now place the code below into Form1 and repeat the test. Much nicer! This worked in VB 2010 Express.

Delegate Sub AutoScrollPositionDelegate(ByVal sender As ScrollableControl, ByVal p As Point)
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter

   Dim p As Point = Panel1.AutoScrollPosition
   Dim del As AutoScrollPositionDelegate = New AutoScrollPositionDelegate(AddressOf SetAutoScrollPosition)

   Panel1.BeginInvoke(del, {Panel1, p})

End Sub
Private Sub SetAutoScrollPosition(ByVal sender As ScrollableControl, ByVal p As Point)

   p.X = Math.Abs(p.X)
   p.Y = Math.Abs(p.Y)
   sender.AutoScrollPosition = p

End Sub
绝影如岚 2024-10-24 11:11:32

如果您有一个比其容器宽得多的控件(例如 TextBox),并且您滚动到它的末尾,然后单击该控件,您将滚动回到 Location 的控制。

单击的控件获得焦点并自动发生滚动,这是 winform 的标准行为。

如果您想否定这一点,则必须拦截容器 (ScrollableControl) 的 SetAutoScrollPosition 或使用其他机制恢复到原始位置。

如果控件已获得焦点,然后您滚动,则再次单击它不会更改容器的 AutoScrollPosition

If you have a control (like a TextBox) that is much wider than it's container and you scroll to it's end, then click the control, you will be scrolled back to the Location of the control.

The clicked control gains focus and the scrolling occurs automatically, that is standard behaviour of winforms.

If you want to negate that, you will have to intercept SetAutoScrollPosition of the container (ScrollableControl) or use another mechanism to revert to the original position.

If the control already has focus and you then scroll, clicking it again won't change the AutoScrollPosition of the container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文