如何在vb.net中创建多个计时器处理程序

时间:2014-01-20 08:53:35

标签: vb.net datagridview timer handler

  • 我必须创建多个计时器" n"处理程序的次数。
  • 将在我的DataGrid中存储一行。
  • 对于每一行,都会有一个单独工作的计时器。

我想到的是:

Private Sub CreateTimer()
    Dim tmr As New Timer
    tmr.Interval = 1000 '1 Second
    tmr.Enabled = True
    AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub

'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(TheTimer as Timer, ByVal sender As Object, ByVal e As EventArgs)
    mynumber = mynumber + 1
    With DataGridView1
        .Rows(n).Cells(4).Value = mynumber " saniye"
    End With
End Sub

那我怎么能实现这个目标呢?

1 个答案:

答案 0 :(得分:2)

我相信Timer的Tag属性可以很好地适用于你的情况。我目前没有我的IDE,但下面的代码片段可以给你这个想法。

Private Sub CreateTimer()
    Dim tmr As New Timer
    tmr.Interval = 1000 '1 Second
    tmr.Enabled = True
    tmr.Tag = ROW_INDEX
    AddHandler tmr.Tick, AddressOf GlobalTimerTick
End Sub

'A timer tick handler that would work for each timer I add with the sub above
'All timers I created should work seperately
Private Sub GlobalTimerTick(ByVal sender As Object, ByVal e As EventArgs)
    mynumber = sender.Tag
    With DataGridView1
        .Rows(n).Cells(4).Value = mynumber " saniye"
    End With
End Sub