CountDown计时器不停止

时间:2016-06-23 21:24:13

标签: wpf vb.net countdown

我已经获得了进度条填充,一旦达到StartTime,进度条和标签都告诉%work。 我无法再按一个按钮说"停止" Dim Timer不允许我从当前功能外部调用它

寻找任何关于停止的建议。 我试过让它包含If

ProgressBar1.Value = Maximum then Timer.Stop() End if

但计时器将继续滴答作响并且不会停止。

Imports System.Threading
Imports System.Windows.Threading.DispatcherTimer
Imports System.Timers
Imports System.Math

Class MainWindow

Private Sub Rectangle_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
    Me.DragMove()
End Sub

Private Sub button_Click(sender As Object, e As RoutedEventArgs)
    Me.Close()
End Sub

Private Sub ext_button_Click(sender As Object, e As RoutedEventArgs) Handles ext_button.Click
    Me.Close()
End Sub

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

    Dim dispatcherTimer = New Threading.DispatcherTimer()



End Sub

Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    Dim StartTime = (Convert.ToInt32(TextBoxHour1.Text) * 3600 + Convert.ToInt32(TextBoxMinute1.Text) * 60 + Convert.ToInt32(TextBoxSecond1.Text))
    LabelST.Content = "Starting Seconds: " + Convert.ToString(StartTime)
    LabelST2.Content = ProgressBar1.Value
    ProgressBar1.Value = ProgressBar1.Value + 1
    Dim percent As Int32 = Convert.ToString(ProgressBar1.Value / StartTime * 100)
    LabelPercent1.Content = Convert.ToString(Math.Floor(percent)) + "%"




    ' Forcing the CommandManager to raise the RequerySuggested event
    CommandManager.InvalidateRequerySuggested()
End Sub

Private Sub ButtonStart_Click(sender As Object, e As RoutedEventArgs) Handles ButtonStart.Click
    Dim Timer = New Threading.DispatcherTimer()
    Dim StartTime = (Convert.ToInt32(TextBoxHour1.Text) * 3600 + Convert.ToInt32(TextBoxMinute1.Text) * 60 + Convert.ToInt32(TextBoxSecond1.Text))
    AddHandler Timer.Tick, AddressOf Timer_Tick
    Timer.Interval = New TimeSpan(0, 0, 1)
    If ButtonStart.Content = "START" Then
        ProgressBar1.Maximum = StartTime
        Timer.Start()
        LabelPercent1.Content = (StartTime / 100)
    End If
End Sub

Sub ButtonStart_Copy_Click(sender As Object, e As RoutedEventArgs) Handles ButtonStart_Copy.Click

End Sub
End Class

2 个答案:

答案 0 :(得分:0)

由于Value为double,您可能需要检查> =而不是equals。浮点数据类型有时可能看起来相等,即使它们不是。

答案 1 :(得分:0)

创建布尔类型的全局字段IsTimerRunning,单击开始按钮时将其设置为true,单击停止按钮时将其设置为false。让计时器滴答回调方法检查此字段,并在其为真时执行正常操作,否则停止计时器。