label.enabled和label.visible将不会在System.Windows.Forms.timer.tick事件VB.NET上更新

时间:2014-02-12 23:48:54

标签: vb.net winforms timer label

任何人都可以让我知道我在哪里出错吗?当blinkTimer应该击中它的嘀嗒事件时,标签不会产生闪烁效果。它的行为几乎就像tick事件永远不会被触发或blinkTimer.Start()实际上没有启动计时器一样。

此代码采用Windows格式,因此您需要将以下内容添加到表单设计器中: label,DateTimePicker,Button

并在属性面板中设置以下内容:

label name = timerLabel
DatetimePicker name = timePicker
button name = startStop

timerLabel.text = "00:00:00"
timePicker.Format = Custom
timePicker.CustomerFormat = "  HH  :  mm  :  ss"
timePicker.ShowUpDown = True
timePicker.Value = 2/12/2014 1:00 AM
startStop.text = "Start"

(我认为我设置的所有属性都是相关的)

以下是大块代码:

Imports System.Windows.Forms
Imports System.Threading
Imports System.Math
Imports System.Timers

Public Class stopwatch
    Private timeTimer As System.Windows.Forms.Timer = New System.Windows.Forms.Timer()
    Private length As TimeSpan = New TimeSpan(1, 0, 0)
    Private formatTimer As System.Timers.Timer
    Private blinkStatus As Boolean = False
    Private WithEvents blinkTimer As System.Windows.Forms.Timer = New System.Windows.Forms.Timer()


    Private Sub setTimeLength()
        length = New TimeSpan(Me.timePicker.Value.Hour, Me.timePicker.Value.Minute, Me.timePicker.Value.Second)
    End Sub

    Private Sub startStop_Click(sender As Object, e As EventArgs) Handles startStop.Click
        setTimeLength()
        initializeTimeTimer()
        setFormatTimer((length.TotalMilliseconds / 4) * 3)
        timeTimer.Start()
        formatTimer.Start()
    End Sub

    Private Sub initializeTimeTimer()
        timeTimer.Interval = 1000
        AddHandler timeTimer.Tick, AddressOf timetimer_tick
    End Sub

    Private Sub timetimer_tick(Optional sender As Object = Nothing, Optional e As System.EventArgs = Nothing)
        If length.TotalSeconds = 0 Then timerLabel.ForeColor = Color.Red
        timerLabel.Text = length.ToString
        length = length.Subtract(New TimeSpan(0, 0, 1))
    End Sub

    Private Sub timePicker_TextChanged(sender As Object, e As EventArgs) Handles timePicker.TextChanged
        Dim hours As String = Me.timePicker.Value.Hour
        Dim minutes As String = Me.timePicker.Value.Minute
        Dim seconds As String = Me.timePicker.Value.Second
        If Len(hours) = 1 Then hours = "0" & hours
        If Len(minutes) = 1 Then minutes = "0" & minutes
        If Len(seconds) = 1 Then seconds = "0" & seconds
        timerLabel.Text = String.Format("{0}:{1}:{2}", hours, minutes, seconds)
    End Sub

    Private Sub stopwatch_Load(sender As Object, e As EventArgs) Handles Me.Load
        timerLabel.Text = length.ToString
        blinkTimer.Interval = 500

    End Sub

    Private Sub setFormatTimer(ByVal intervalValue As Integer)
        formatTimer = New System.Timers.Timer(intervalValue)
        AddHandler formatTimer.Elapsed, AddressOf formatTimer_elapsed
    End Sub

    Private Sub formatTimer_elapsed(sender As Object, e As ElapsedEventArgs)
        If timerLabel.ForeColor = Color.Lime Then
            timerLabel.ForeColor = Color.Yellow
            formatTimer.Stop()
            setFormatTimer((length.TotalMilliseconds / 3) * 2)
            formatTimer.Start()
        ElseIf timerLabel.ForeColor = Color.Yellow Then
            timerLabel.ForeColor = Color.Red
            formatTimer.Stop()
            setFormatTimer(length.TotalMilliseconds)
            formatTimer.Start()

        ElseIf timerLabel.ForeColor = Color.Red Then
            formatTimer.Stop()
            blinkTimer.Start()
            Console.WriteLine("blinking should be started")
            blinkStatus = True
        End If
    End Sub

    Private Sub blinkTimer_tick(Optional sender As Object = Nothing, Optional e As EventArgs = Nothing) Handles blinkTimer.Tick
        Console.WriteLine("blinkTimer.tick is a go")
        If timerLabel.Enabled = True Then
            timerLabel.Visible = False
            timerLabel.Enabled = False
        Else
            timerLabel.Visible = True
            timerLabel.Enabled = True
        End If    
    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

我会尝试删除InvokeRequired部分,看看是否有效,或者将一个MsgBox放在if语句之上,看它是否甚至触发了tick事件。