如何更新标签以显示另一个表格的秒表?

How to update label to display a Stopwatch form another Form?

我有一个项目,其中有一个带有用于秒表的计时器的表单和另一个带有用于显示它的标签的表单。从第二个表格开始,在我按下“接受”的那一刻,它向第一个表格发送了一些数据,因此计时器可以正常工作。之后,从第一种形式开始,计时器应该工作并更新第二种形式的标签文本。问题是 Label 不显示秒表,但 Timer 工作。

我需要这样做的原因是即使 2nd Form 已关闭,定时器仍能正常工作。当它再次打开时,它必须显示秒表减去已经过去的时间。

这是第 1 形式的代码。

Public startTime As DateTime
Public elapsedTime As TimeSpan

Private ReadOnly duration As TimeSpan = TimeSpan.FromMinutes(30)

Private Sub tmStopwatch_Tick(sender As Object, e As EventArgs) Handles tmStopwatch.Tick

    elapsedTime = DateTime.Now.Subtract(startTime)

    If elapsedTime >= duration Then
        elapsedTime = duration
        tmStopwatch.Stop()
    End If
    UpdateStopwatchDisplay()

End Sub

Public Sub UpdateStopwatchDisplay()
    Dim frmAS As New frmAddSchedule

    frmAH.lblDisplayStopwatch.ForeColor = If(tmStopwatch.Enabled, Color.RoyalBlue, Color.DarkGray)
    frmAH.lblDisplayStopwatch.Text = (duration - elapsedTime).ToString("mm\:ss\.ff")

End Sub

这是发送数据的第二个表格:

Private Sub btnAccept_Click(sender As Object, e As EventArgs) Handles btnAccept.Click

    Dim frmD As New frmDashboard

    frmD.startTime = DateTime.Now.Subtract(frmD.elapsedTime )
    frmD.tmStopwatch.Enabled = True

    frmD.UpdateStopwatchDisplay()
    frmD.tmStopwatch.Start()

End Sub

在此先感谢您的帮助。

编辑: 我想做的是为列表(在表 2 中)设置一个 30 分钟的倒数计时器。为了让用户看到倒计时,我希望 Form 2 通过执行 Form 1 中的过程来显示它。这就像在后台执行该过程,如果用户出于任何原因关闭了 form 2,该表单将能够在剩余时间显示。

你的做法大错特错。在第一种形式中,您不需要任何与时间有关的东西。第二种形式可以搞定一切。下面是每次使用第二种形式的相同实例并通过调用 ShowDialog.

显示它的示例

表格 1:

Private dialogue As Form2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Create the dialogue form the first time the Button is clicked.
    If dialogue Is Nothing Then
        dialogue = New Form2
    End If

    'Display the second form as a modal dialogue.
    dialogue.ShowDialog()
End Sub

Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    'If the second form was created, dispose it when this form is disposed.
    dialogue?.Dispose()
End Sub

表格 2:

'Measures elapsed time.
Private stopwatch As New Stopwatch

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Start measuring elapsed time and start displaying it.
    stopwatch.Start()
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Display the elapsed time.
    Label1.Text = stopwatch.Elapsed.ToString()
End Sub

Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    'Don't measure or display elapsed time when form is not displayed.
    stopwatch.Stop()
    Timer1.Stop()
End Sub

第二个表格现在将测量并显示自己经过的时间。只要表单未显示,秒表就会暂停。

如果你想调用 Show 而不是 ShowDialog,事情会稍微复杂一些,因为关闭该表单将处理它并需要在下次你想显示时创建一个新的它。如果那是您想要的,请告诉我,我会扩展示例。

编辑:

为了使用 Show 而不是 ShowDialog,还需要进行一些其他更改。由于不会每次都使用同一个实例,因此我们需要一些机制来记住两者之间经过的时间。有一些特定实现的选项,但最简单的是只创建 Stopwatch 对象 Shared。这样,每个实例都将使用相同的 Stopwatch,因此每个实例的运行时间都相同:

表格 1:

Private dialogue As Form2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If dialogue Is Nothing OrElse dialogue.IsDisposed Then
        'Create and display a new dialogue.
        dialogue = New Form2
        dialogue.Show()
    Else
        'Set focus to the existing dialogue.
        dialogue.Activate()
    End If
End Sub

表格 2:

'Measures elapsed time.
Private Shared ReadOnly stopwatch As New Stopwatch

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Start measuring elapsed time and start displaying it.
    stopwatch.Start()
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Display the elapsed time.
    Label1.Text = stopwatch.Elapsed.ToString()
End Sub

Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    'Don't measure or display elapsed time when form is not displayed.
    stopwatch.Stop()
    Timer1.Stop()
End Sub

在这种情况下,技术上可以创建 Form2 的多个实例,尽管 Form1 中的代码被编写为一次只能创建一个实例。您实际上可以将 Form2 实现为 Singleton,这意味着不可能创建多个实例:

表格 1:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Create and display a new instance or focus the existing instance.
    Form2.Instance.Show()
    Form2.Instance.Activate()
End Sub

表格 2:

'Measures elapsed time.
Private Shared ReadOnly stopwatch As New Stopwatch

'The one and only instance of the class.
Private Shared _instance As Form2

'Make the constructor Private so that an instance cannot be created externally.
Private Sub New()
    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
End Sub

'The one and only instance of the class.
Public Shared ReadOnly Property Instance As Form2
    Get
        'If there is no current instance or the current instance has been disposed, create a new instance.
        If _instance Is Nothing OrElse _instance.IsDisposed Then
            _instance = New Form2()
        End If

        Return _instance
    End Get
End Property

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Start measuring elapsed time and start displaying it.
    stopwatch.Start()
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    'Display the elapsed time.
    Label1.Text = stopwatch.Elapsed.ToString()
End Sub

Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    'Don't measure or display elapsed time when form is not displayed.
    stopwatch.Stop()
    Timer1.Stop()
End Sub