vb.net 如何让标签闪烁

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42970996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:13:43  来源:igfitidea点击:

How to make a label blink

vb.netwinformstimer

提问by Eaten Taik

I have a Stopwatchin my form with Interval = 1000displayed in the hh:mm:ssformat.

Stopwatch我的表单中有一个以格式Interval = 1000显示hh:mm:ss

When it reaches the 5th second it should start to blink the label background as green but so far I can only make the background color turn to green without any flash.

当它到达第 5 秒时,它应该开始将标签背景闪烁为绿色,但到目前为止我只能使背景颜色变为绿色而没有任何闪光。

This is how I turn the background color to green:

这就是我将背景颜色变为绿色的方法:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
    If Label1.Text = "00:00:05" Then
        Label1.BackColor = Color.Green
    End If
End Sub

How do I make the label blink?

如何让标签闪烁?

回答by Bugs

You could use a simple Asyncmethod to do this.

您可以使用一种简单的Async方法来做到这一点。

The following code will give Label1the effect of flashing. Since we have used While Truethis will continue indefinitely once you hit "00:00:05".

下面的代码会给出Label1闪烁的效果。由于我们已经使用While True过,一旦您点击“00:00:05”,这将无限期地继续。

Private Async Sub Flash()
    While True
        Await Task.Delay(100)
        Label1.Visible = Not Label1.Visible
    End While
End Sub

You would call this inside your Timer1_Tickmethod:

你会在你的Timer1_Tick方法中调用它:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
    If Label1.Text = "00:00:05" Then
        Label1.BackColor = Color.Green
        Flash()
    End If
End Sub

Ifyou only want to flash a couple of times we can make a simple change to Flash():

如果您只想闪烁几次,我们可以对以下内容进行简单更改Flash()

Private Async Sub Flash()
    For i = 0 To 10
        Await Task.Delay(100)
        Label1.Visible = Not Label1.Visible
    Next

    'set .Visible to True just to be sure
    Label1.Visible = True
End Sub

By changing the number 10 to a number of your choice you can shorten or lengthen the time taken to flash. I have added in Label1.Visible = Trueafterthe Forloop just to be sure that we see the Labelonce the flashing has finished.

通过将数字 10 更改为您选择的数字,您可以缩短或延长闪光所需的时间。我在循环Label1.Visible = True之后添加了For只是为了确保我们Label在闪烁完成后看到。

You will have to import System.Threading.Tasksto make use of Task.Delay.

您必须导入System.Threading.Tasks才能使用Task.Delay.

回答by keerti

Try to put something like this in Timer1_Tick event handler -

尝试在 Timer1_Tick 事件处理程序中放置类似的内容 -

Label1.Visible = Not Label1.Visible

Set the timer to enabled and it will do the job.

将计时器设置为启用,它将完成这项工作。

回答by Dave B

If you specify the color when the Text is 00:00:05then you should also specify what the Backcolorshould be when the text is something else i.e 00:00:06

如果您在文本为00:00:05时指定颜色,那么您还应该指定Backcolor当文本为其他内容时应该是什么,即00:00:06

Try this and see if it works:

试试这个,看看它是否有效:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = SW.Elapsed.ToString("hh\:mm\:ss")
If Label1.Text = "00:00:05" Then
    Label1.BackColor = Color.Green
else
    Label1.Backcolor = Color.Yellow '(Change color as needed)
End If
End Sub