【问题标题】:How to make a picturebox to blink如何使图片框闪烁
【发布时间】:2021-02-09 21:57:06
【问题描述】:

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

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

它第一次完成它的工作,但是从第二次下载完成开始,它只显示图片框而不闪烁..如果闪烁 = 5(显然在停止计时器之后),我尝试将闪烁 = 0,但它确实一样。 我怎样才能让它在第一次之后也闪烁?谢谢

【问题讨论】:

    标签: vb.net winforms timer picturebox


    【解决方案1】:

    在每个 Button.Click 事件中,您正在向 Timer.Tick 事件添加一个新的处理程序。
    每个定时器 Timer 都会重新启动,所有事件处理程序都会在相同的时间间隔内被调用,因此 Control Visible 属性会同时设置为 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.Load 事件)中订阅一次Tick 事件:

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

    Form 关闭时移除 Handler 并处理 Timer:

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

    【讨论】:

    • 谢谢!这很有用!
    【解决方案2】:

    附带说明,如果您将ReadOnly 更改为WithEvents,那么您可以去掉AddHandler 语句并在Tick() 事件结束时切换到Handles 子句:

    WithEvents timerblinking As New Windows.Forms.Timer
    
    Private Sub timerblinking_Tick(sender As Object, e As EventArgs) Handles timerblinking.Tick
    
    End Sub
    

    然后,您不会遇到针对单个事件多次触发相同方法的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2015-01-22
      相关资源
      最近更新 更多