[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

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
0
Jimbo99999
Asked:
Jimbo99999
3 Solutions
 
melmersCommented:
lblStatus7.Caption = "Email complete..."
System.Threading.Thread.Sleep(3000) ' display for 3 seconds
lblStatus7.Caption = " "
0
 
jpaulinoCommented:
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 TomlinsonMiddle 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jpaulinoCommented:
>> 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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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