如何让标签闪烁

问题描述 投票:0回答:5

我的表单中有一个

Stopwatch
,其中
Interval = 1000
hh:mm:ss
格式显示。

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

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

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

如何让标签闪烁?

vb.net winforms timer
5个回答
8
投票

您可以使用简单的

Async
方法来执行此操作。

下面的代码将给出

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

您可以在您的

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

如果您只想闪烁几次,我们可以对

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

通过将数字 10 更改为您选择的数字,您可以缩短或延长闪烁时间。我在

Label1.Visible = True
after 中添加了
For
循环,只是为了确保闪烁完成后我们能看到
Label

您必须导入

System.Threading.Tasks
才能使用
Task.Delay


1
投票

您需要一个标签、两个文本框和一个按钮。 屏幕允许您“设置”几种颜色 - 这可以通过添加错误颜色、警告颜色(您尚未填写...中的字段?)等来进一步实现。 在实际应用程序中,这种颜色选择将由管理员从单独的屏幕完成,并存储在数据库中。 计时器频率也可以在管理屏幕/功能中设置。 这个特定的屏幕需要双击文本框,并为每个文本框选择一种颜色。 每个框的背景颜色都会发生变化。然后按开始按钮。 如果您再次按下“开始”按钮,则会切换计时器(开/关)

公开课表格1 Private Sub Form1_Load(sender As Object, e As EventArgs) 处理 MyBase.Load ' 不太符合我想要的,但接近...... ' https://bytes.com/topic/visual-basic-net/answers/368433-blinking-text Me.Label1.Text = "闪烁的文本框" Me.Label1.BackColor = TextBox2.BackColor 结束子

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If Me.Label1.BackColor = TextBox2.BackColor Then
        Me.Label1.BackColor = TextBox1.BackColor
    Else
        Me.Label1.BackColor = TextBox2.BackColor
    End If
End Sub

Private Sub TextBox1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox1.MouseDoubleClick
    Dim dlg As New ColorDialog()
    If dlg.ShowDialog() = DialogResult.OK Then
        TextBox1.BackColor = dlg.Color
    End If
End Sub

Private Sub TextBox2_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles TextBox2.MouseDoubleClick
    Dim dlg As New ColorDialog()
    If dlg.ShowDialog() = DialogResult.OK Then
        TextBox2.BackColor = dlg.Color
    End If
End Sub

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    Timer1.Enabled = Not Timer1.Enabled
End Sub

下课


0
投票

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

Label1.Visible = Not Label1.Visible

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


0
投票

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

Backcolor
应该是什么,即 00:00:06

尝试一下,看看是否有效:

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

0
投票

另一个解决方案:

Public Class Form1
Private blinkingTimer As New Timer()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Set Timer
    blinkingTimer.Interval = 500 ' Time to keep blinking in milliseconds
    AddHandler blinkingTimer.Tick, AddressOf BlinkLabel

    ' Start Timer
    blinkingTimer.Start()
End Sub

Private Sub BlinkLabel(sender As Object, e As EventArgs)
    If Label1.ForeColor = Color.Red Then
        Label1.ForeColor = Color.Black
    Else
        Label1.ForeColor = Color.Red
    End If
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    ' Stop timer when app is closed
    blinkingTimer.Stop()
End Sub

下课

© www.soinside.com 2019 - 2024. All rights reserved.