65.9K
CodeProject is changing. Read more.
Home

Auto Clearing Label by Extension

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (2 votes)

Apr 15, 2016

CPOL

1 min read

viewsIcon

5281

downloadIcon

41

This message will self destruct in 5 seconds...

Introduction

Often, when programming a GUI, you will use a statusbar with some sort of Label. And often, you want to log a message to that Label and not worry about the message staying visible until your code manages to clear or change it. Instead, during a process, you want to log some status message which makes sense (you hope) in the current context, but disappears after some seconds. Also, you don't want to bother to sub-class the Label control into a new control. 

If all of the above apply to you, then read on because here is a tip to use an Extension to AutoClear a Label.Text.

Using the Code

Life can be very simple: in your (Windows or other) application, create a Module called Extensions or something similar, and define the following extension methods:

Imports System.Runtime.CompilerServices

Public Module Extensions
    
    'overload 1: initialise and set timer
    <Extension()>
    Public Sub TextAutoClear(this As Label, txt As String, clearAfterNrMils As Integer)
        Try
            Dim mytimer As New System.Timers.Timer(clearAfterNrMils)
            AddHandler mytimer.Elapsed, AddressOf this.TextAutoClear
            mytimer.AutoReset = False
            mytimer.Enabled = True
            
            this.Text = txt
            this.Update
        Catch exc As Exception
            'Debug.Print(exc.ToString)
            Return
        End Try
    End Sub
    
    'overload 2: timer event is triggered, auto clears and cleans up
    <Extension()>
    Public Sub TextAutoClear(this As Label, sender As Object, e As System.Timers.ElapsedEventArgs)
        Try
            Dim mytimer As System.Timers.Timer = CType(sender,System.Timers.Timer)
            RemoveHandler mytimer.Elapsed, AddressOf this.TextAutoClear
            
            If this.InvokeRequired Then
                this.Invoke(New MethodInvoker(Sub() this.Text = ""))
            Else
                this.Text = ""
            End If
            
            'clean up
            mytimer.Enabled = False
            mytimer.Dispose
            mytimer = Nothing
        Catch
            'Debug.Print(exc.ToString)
            Return
        End Try
    End Sub
    
    'overload 3: just a short-cut to overload 1
    <Extension()>
    Public Sub TextAutoClear(this As Label, txt As String)
        this.TextAutoClear(txt,3000)
    End Sub
    
End Module

TextAutoClear overload method 1 sets the Text message and initializes a Timer to go off after clearAfterNrMils milliseconds. The timer will go off only once because I set its AutoReset to False. When the bell tolls after clearAfterNrMils, then TextAutoClear overload method 2 is triggered. This method will convert the sender to a Timer; remove the handler installed in overload 1 and clean itself up at the end of the method. Because the Timer fires on a different thread, you have to use Invoke with an anonymous method, inline method. The anonymous method in this case is simply: Sub() this.Text = "".

And that's it! Use it like this in your main (or other form):

Public Partial Class MainForm
    Public Sub New()
        Me.InitializeComponent()
        
        labAutoClear.TextAutoClear("This message will self destruct after 5 seconds...",5000)
        
    End Sub
End Class

History

This mental child was conceived during coding in March 2016.