Click here to Skip to main content
15,881,711 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Auto Clearing Label by Extension

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
14 Apr 2016CPOL1 min read 5.1K   41   5  
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:

VB.NET
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):

VB.NET
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer VeeTools
Netherlands Netherlands
A (usually exploring) geologist who sometimes develops software for fun and colleagues... Check out my new website at www.veetools.xyz for online mapping and coordinate conversion.

Comments and Discussions

 
-- There are no messages in this forum --