如何让Timer根据它的启动方式做不同的事情

时间:2016-11-05 22:56:32

标签: vb.net timer

如何让我的Timer做不同的事情取决于激活它的是什么?我尝试过使用这段代码

Dim a As Integer = 0
Dim b As Integer = 0

Private Sub Button1_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover
    Timer1.Start
End Sub

Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
    Timer1.Start
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
    If sender Is Button1 Then
        a = a + 1
        TextBox1.Text = a
    End If

    If sender Is Button2 Then
        b = b + 1
        TextBox2.Text = b
    End If
End Sub

但是Textbox只添加1次。这意味着定时器只是像Timer一样不连续地执行一次。那么我在那里做错了什么,或者我可以做些不同的事情?

3 个答案:

答案 0 :(得分:1)

另一种可能更简单的方法是使用Timer.Tag属性并为两个按钮使用一个处理程序:

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
    Timer1.Tag = sender
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    a += 1
    If Timer1.Tag Is Button2 Then
        TextBox1.Text = a.ToString
    End If
    If Timer1.Tag Is Button1 Then
        TextBox2.Text = a.ToString
    End If
End Sub

由于Tag已经是Object类型,因此不需要外部转换。

我包含了Start功能,因为我不确定你最初是如何启动计时器的。如果你以不同的方式做,它可以被排除在按钮事件处理程序

之外

答案 1 :(得分:0)

我不确定这有什么价值,一般来说,这不是人们做的事情,但是如果你想知道哪个动作激活了计时器,你需要注册它

Private  _timer As Timer
Private _activatingControl As Object

Private Sub ActivateTimer(c as Object)
    _activatingControl = c ' this is first
    _timer.Start()
End Sub

Private Sub Button2_MouseHover(sender As Object, e As EventArgs) Handles Button2.MouseHover
    ActivateTimer(sender)
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick, Button2.MouseHover, Button1.MouseHover
    . . . . . . 
    System.Diagnostics.WriteLine("timer was activated by" + _activatingControl.ToString())

    If DirectCast(_activatingControl, Control).Name = "Button1" Then 
        . . . .
    ElseIf DirectCast(_activatingControl, Control).Name = "Button2" Then
        . . . . 
    End If
End Sub

因此,您始终需要通过ActivateTimer激活计时器。 Timer1_Tick(sender As Object永远是定时器本身

答案 2 :(得分:0)

我喜欢tinstaafl的Tag属性方法。类似的方法是创建一个继承自System.Windows.Forms.Timer的自定义计时器,并具有一个Button属性,可以在按钮的MouseHover事件处理程序中设置。

计时器的Start方法只需要调用一次,因此我将其移动到表单的加载事件处理程序中。我不确定你想要实现的目标。根据这一点,计时器的Start方法可以保留在按钮MouseHover事件处理程序中,Timer1.Stop可以在计时器的tick事件处理程序结束时调用。然后,这将仅响应每个MouseHover事件递增计数器(a)的值一次。或者可以在按钮中调用Timer1.Stop。 MouseLeave事件,如果您只希望计数器在鼠标悬停在按钮上时递增。

Public Class Form1

Private Class CustomTimer
Inherits System.Windows.Forms.Timer

Private m_myButton As Button

Public Property Button() As Button
    Get
        Return m_myButton
    End Get
    Set(ByVal value As Button)
        m_myButton = value
    End Set
End Property

End Class

Private WithEvents Timer1 As New CustomTimer
Private a As Integer

Private Sub Form1_Load(sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Timer1.Interval = 100
    Timer1.Start()
End Sub

Private Sub Button_MouseHover(sender As Object, e As EventArgs) Handles Button1.MouseHover, Button2.MouseHover
  Timer1.Button = DirectCast(sender, Button)
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  a += 1
  If Timer1.Button Is Button1 Then 
      TextBox1.Text = a.ToString
  ElseIf Timer1.Button Is Button2 Then 
      TextBox2.Text = a.ToString
  End If
End Sub

End Class