如何使图片框闪烁

How to make a picturebox to blink

我正在下载一个文件,最后,我让一个图片框闪烁了几下。 在闪烁结束时,图片框应该隐藏自己。 我正在使用此代码:

ReadOnly timerblinking As New Windows.Forms.Timer
dim blinking as integer
Private Sub mClient_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles mclient.DownloadFileCompleted

       Label2.Text = "Downloaded"
       Label2.Refresh()
       blinking = 0
       timerblinking.Interval = 500                                                           
       timerlampeggio.Enabled = True                                                           
       AddHandler timerblinking.Tick, AddressOf Timer_tick

   End Sub
Private Sub Timer_tick(sender As Object, e As EventArgs)

      blinking += 1
       Rapid.PictureBox2.Visible = Not Rapid.PictureBox2.Visible
       If blinking= 5 Then                                                                                 '
           timerblinking.Stop()
           Rapid.PictureBox2.Visible = False
       End If
   End Sub

它是第一次工作,但是从第二次下载完成开始,它只显示图片框而不闪烁..我试着把 blinking=0 if blinking=5 (显然是在停止计时器之后)但是它做同样的事情。 我怎样才能让它在第一次之后也闪烁?谢谢

附带说明一下,如果您在 Tick() 事件结束时将 ReadOnly 更改为 WithEvents, then you can get rid of the AddHandler statement and switch to a Handles 子句:

WithEvents timerblinking As New Windows.Forms.Timer

Private Sub timerblinking_Tick(sender As Object, e As EventArgs) Handles timerblinking.Tick

End Sub

那么您就不会 运行 遇到同一事件多次触发同一方法的问题。

在每个 Button.Click 事件中,您要向 Timer.Tick 事件添加一个新的处理程序。
每次重新启动计时器时,所有事件处理程序都会在相同的时间间隔内被调用,因此控件可见 属性 被同时设置 true/false 多次。

这当然有一个不太好的效果:它可能只是消失,或者随机出现然后消失,这取决于定时器频率(间隔)。

每次单击该按钮并重新启动计时器时,您都可以从 Tick 处理程序测试此写入输出窗格的操作:

Private Timer1 As System.Windows.Forms.Timer = New System.Windows.Forms.Timer

Private Sub someButton_Click(sender As Object, e As EventArgs) Handles someButton.Click
    AddHandler Timer1.Tick, AddressOf Timer1_Tick
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs)
    Console.WriteLine("Timer Ticked")
    Timer1.Stop()
End Sub

您会在“输出”窗格中看到:

Timer Ticked

Timer Ticked
Timer Ticked

Timer Ticked
Timer Ticked
Timer Ticked

在 Form Constructor(或 Form.Load 事件)中订阅一次 Tick 事件:

Public Sub New()
    AddHandler Timer1.Tick, AddressOf Timer1_Tick
End Sub

在窗体关闭时删除处理程序并处理计时器:

Private Sub SomeForm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
    RemoveHandler Timer1.Tick, AddressOf Timer1_Tick
    Timer1.Dispose()
End Sub