【问题标题】:How to run multiple timer in a form?如何在一个表单中运行多个计时器?
【发布时间】:2016-12-02 15:56:03
【问题描述】:

我的表单中有一个包含 50 个按钮和 1 个标签的面板

Private Sub flp_table_paint(sender As Object, e As PaintEventArgs) Handles flp_table.Paint

        Dim i As Integer
        For i = 1 To 50
            Dim btn_tableNo As New Button
            btn_tableNo.Width = 40
            btn_tableNo.Height = 40
            btn_tableNo.Text = i
            AddHandler btn_tableNo.Click, AddressOf TableButtonClicked

            Dim timer As New Timer
            timer.Tag = i

            Me.flp_table.Controls.Add(btn_tableNo)
        Next

End Sub

我尝试做的是,对于我单击的每个按钮,它们都会启动自己的计时器并显示在标签上。

例子:

11:00:00PM - 点击 Button1 ,lb_timer 将显示 1,2,3,4...

11:00:30PM - 点击 Button2 ,lb_timer 将显示 1,2,3,4...

11:00:45PM - 再次点击 Button1,lb_timer 将显示 45,46,47,48...

11:00:50PM - 再次点击 Button2,lb_timer 将显示 20,21,22,23...

这是我目前尝试的,但失败了......

Private Sub TableButtonClicked(ByVal sender As Object, ByVal e As EventArgs)
    Dim currButton As Button = sender
    selectedTable = currButton

    For Each timer As Timer In Me.Controls
        If timer.Tag = selectedTable.Text Then
            timer.Start()
            lb_timer.Text = timer.ToString
        End If
    Next
End Sub

我不知道如何使它工作,请帮助我...

【问题讨论】:

  • 你不应该在绘画活动中做任何事情,除了绘画。这也是一个危险的计时器,因为您没有在表单范围内声明它,所以垃圾收集器不知道如何清理它。您也不会对计时器做任何事情——它肯定不是您稍后要循环的 Controls 集合的一部分。计时器不是控件,它们是组件。

标签: vb.net timer


【解决方案1】:

这里的异步方法没有Timer 和所有按钮的一个事件处理程序

' In constructor
AddHandler button1.Click, AddressOf Button_Click
AddHandler button2.Click, AddressOf Button_Click
AddHandler button3.Click, AddressOf Button_Click
' ... rest of buttons


' variable will keep name of the button which number is showing
Private _selectedButtonName As String

Private Async Sub Button_Click(sender As object, e As EventArgs)
    Dim button As Button = DirectCast(sender, Button)
    _selectedButtonName = button.Name

    Dim isRunning As Boolean = (button.Tag IsNot Nothing)
    If isRunning = True Then return

    await StartCounterAsync(button)
End Sub

Private Async Function StartCounterAsync(Button button) As Task
    button.Tag = new object()
    Dim number As Integer = 0
    While True
        await Task.Delay(1000)
        number += 1

        If _selectedButtonName.Equals(button.Name)
            lb_timer.Text = $"{button.Name}: {number}"
        End If
    End While
End Function

您可以添加CancellationToken 以防您需要重置计数器。

【讨论】:

    【解决方案2】:

    遗憾的是,计时器不显示经过的时间。一个更容易用于您的目的的控件是 StopWatch 控件。它非常相似,并且会显示运行时间。

    Dim StopWatchTest As New Stopwatch
    StopWatchTest.Start()
    Dim EllapsedTime As String = StopWatchTest.ElapsedMilliseconds / 1000
    

    但是,由于您希望使用经过的时间不断更新标签,因此您需要一个计时器来在每次滴答时更新标签。

    Dim UpdateLabelTimer As New Timer()
    UpdateLabelTimer.Interval = 1000 'How often to update your label (in milliseconds)
    AddHandler UpdateLabelTimer.Tick, AddressOf Tick
    UpdateLabelTimer.Start()
    
    ----------
    
    Private Sub Tick()
        lblLabel.text = StopWatchTest.ElapsedMilliseconds
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2017-03-17
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      相关资源
      最近更新 更多