VB.net Label Timer

Good Day

Experts:

I want to put the following message in a lable on my form.  However, I want to display it for 3 seconds and then blank it out.  How can I achieve this?

1) lblStatus7.Caption = "Email complete..."
2) display for 3 seconds
3) lblStatus7.Caption = " "

Thanks,
jimbo99999
Jimbo99999Asked:
Who is Participating?
 
melmersConnect With a Mentor Commented:
lblStatus7.Caption = "Email complete..."
System.Threading.Thread.Sleep(3000) ' display for 3 seconds
lblStatus7.Caption = " "
0
 
Jorge PaulinoConnect With a Mentor IT Pro/DeveloperCommented:
Just a small correction to melmers example:
lblStatus7.Text = "Email complete..."
System.Threading.Thread.Sleep(3000) ' display for 3 seconds
lblStatus7.Text = String.Empty

Open in new window

0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
C'mon guys...you really want to put the MAIN UI thread to sleep for 3 whole seconds?  It may not even display the updated value before becoming unresponsive!...  =\

How about using a Timer?

Here is a simplistic generalized approach:



Public Class Form1
 
    Private Class LabelMessage
 
        Public lbl As Label
        Public msg As String
 
        Public Sub New(ByVal lbl As Label, ByVal afterMsg As String)
            Me.lbl = lbl
            Me.msg = afterMsg
        End Sub
 
    End Class
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ShowMessage(lblStatus7, "Email complete...", 3, "")
    End Sub
 
    Private Sub ShowMessage(ByVal lbl As Label, ByVal msg As String, ByVal displayTimeInSeconds As Integer, ByVal afterMsg As String)
        Dim tmr As New System.Windows.Forms.Timer
        tmr.Tag = New LabelMessage(lbl, afterMsg)
        tmr.Interval = TimeSpan.FromSeconds(displayTimeInSeconds).TotalMilliseconds
        AddHandler tmr.Tick, AddressOf tmr_Tick
        lbl.Text = msg
        tmr.Start()
    End Sub
 
    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tmr As Timer = DirectCast(sender, Timer)
        tmr.Stop()
        If TypeOf tmr.Tag Is LabelMessage Then
            Dim lblMsg As LabelMessage = DirectCast(tmr.Tag, LabelMessage)
            lblMsg.lbl.Text = lblMsg.msg
        End If
    End Sub
 
End Class

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Jorge PaulinoIT Pro/DeveloperCommented:
>> C'mon guys...you really want to put the MAIN UI thread to sleep for 3 whole seconds?  It may not even display the updated value before becoming unresponsive!...  =\

Maybe Idle :)  ... don't know the goal of it!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
At any rate, if you want to use the Sleep() method then you'll need DoEvents() to allow the Label to refresh itself before we "sleep":
(otherwise the Label will simply go blank after three seconds without displaying the message)


lblStatus7.Text = "Email complete..."
Application.DoEvents()
System.Threading.Thread.Sleep(3000) ' display for 3 seconds
lblStatus7.Text = ""

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*or you can explicitly call Refresh before Sleep:
lblStatus7.Refresh()

Open in new window

0
 
Jimbo99999Author Commented:
Thanks for the help...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.