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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

melmersSenior Software Deveolper/ArchitectCommented:
lblStatus7.Caption = "Email complete..."
System.Threading.Thread.Sleep(3000) ' display for 3 seconds
lblStatus7.Caption = " "
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jorge PaulinoIT 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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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 TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
*or you can explicitly call Refresh before Sleep:
lblStatus7.Refresh()

Open in new window

0
Jimbo99999Author Commented:
Thanks for the help...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.