光标滚动RichTextBox时停止计时器

问题描述

我使用的是timer1,当光标位于RTF文本框内时会停止,因此我已经尝试了一些事件(例如Mouse enter,Mouse down和Got focus)来停止计时器,但是当我处于活动状态时,这些事件不起作用滚动RichTextBox。 哪个事件允许我滚动并让计时器1保持关闭状态,因为光标位于richtextBox内部? 谢谢

解决方法

似乎没有简单的单事件方法来执行此操作。您已经发现,单击滚动条时没有EnterMouseEnter事件。我认为以下应该做您想要的事情:

Private isScrollingRtb As Boolean = False

Private Sub RichTextBox1_Enter(sender As Object,e As EventArgs) Handles RichTextBox1.Enter
    'The RTB received focus.
    Timer1.Stop()
End Sub

Private Sub RichTextBox1_Leave(sender As Object,e As EventArgs) Handles RichTextBox1.Leave
    'The RTB lost focus.
    Timer1.Start()
End Sub

Private Sub RichTextBox1_VScroll(sender As Object,e As EventArgs) Handles RichTextBox1.VScroll
    If Not ActiveControl Is RichTextBox1 Then
        'The user scrolled the RTB while it did not have focus.
        Timer1.Stop()
        isScrollingRtb = True
    End If
End Sub

Private Sub Form1_MouseEnter(sender As Object,e As EventArgs) Handles Me.MouseEnter
    If isScrollingRtb Then
        'The user left the RTB after scrolling it without focus.
        Timer1.Start()
        isScrollingRtb = False
    End If
End Sub

当用户单击滚动条时,将引发VScroll事件,因此您可以在该事件上Stop Timer。问题是决定何时再次Start。在此示例中,我选择了当鼠标指针下一个移过表单本身时这样做。