How to a make a label appear for only 3 seconds in VB.NET???

I am using VB.Net. Basically I want a label to appear for only 3 seconds in my code as it executes..

This is what I have...

 Dim timeOut As DateTime = Now.AddMilliseconds(3000) '3 seconds

        lbMsg3.Visible = True
            Do
                lbMsg3.Text = "INVALID TAG NUMBER"
                lbMsg3.ForeColor = Color.FromArgb(205, 38, 38)
            Loop Until Now < timeOut

            'after 3 seconds I want the message to disappear
            lbMsg3.Visible = False

This is the code I have for a label but I want it to disappear after 3 seconds of it displaying....but I don't see it first or last....I suspect it is skipping my DO Loop...

How I pause the application so that I see the lable for 3 seconds and have it disappear once 3 seconds have elapsed.

Any hep or insight would be much appreciated.
nomar2Asked:
Who is Participating?
 
SStoryCommented:
do a lblMsg3.refresh to be sure it displays and
after that do
System.Threading.Thread.Sleep(3000)

This will freeze for 3 seconds (3000ms)

HTH,

Shane
0
 
SStoryCommented:
BTW, get rid of the DO loop if you use the sleep command as I don't think you'll need it.
0
 
ElrondCTCommented:
Do you want the whole program to be frozen for those three seconds, or can the user do something else if he wants to?

If you don't want to freeze, then you should use a Timer control:

    Friend WithEvents TimerDisplay As New System.Windows.Forms.Timer
    TimerDisplay.Interval = 3000
    TimerDisplay.Enabled = False

    Private Sub TimerDisplay_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerDisplay.Tick
        TimerDisplay.Enabled = False                ' Don't display again
        Label1.Visible = False
    End Sub

When you display the label, add:

    TimerDisplay.Enabled = True

This will cause the timer to start counting. When 3 seconds are up, the Tick event will fire, and the label will disappear.
0
 
ElrondCTCommented:
Sorry, I should have looked back at your code to use the right name for the label. Change the second line of the Tick event to:

      lbMsg3.Visible = False

rather than "Label1".
0
 
SStoryCommented:
If you don't want the program frozen, their are several ways to tackle the problem.
You could use a separate thread or the BackgroundWorker component (vb.net 2005) to do this.  Using a timer is just a way to use a cheap simple additional "thread"
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.