• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 865
  • Last Modified:

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.
0
nomar2
Asked:
nomar2
  • 3
  • 2
1 Solution
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now