VB.net: Update Listbox with Time,Text then Autoscroll

When making applications in VB.net I often use a Listbox to give the application user some kind of status updates when a application have to loop through a huge amount of data.
It’s a nice way to be able to tell if it’s still working and doesn’t seem like a non-responding application that have crashed.

Therefor I wrote this little code snippet, it’s basically Sub I got inside my elite-module.vb (//irony) which is filled with handy custom functions, suns and such that I tend to use often in my projects.

Anyway, instead of using ~4 lines of code every time to add some text as a update the Listbox, then autoscrolls down to show the new status item and also do a UI refresh.
If there’s intense CPU load the UI might not respond so well, so we force it to redraw.

Here’s the code snippet:

    ' UpdateStatus (Made for "Status updates". It adds/update the the Current Time + Your text string. Then Scrolls down to new item & Refresh Listbox UI.
    ' Example of usage: UpdateStatus(ListBox1, "I just added a new status update...")

    Public Sub UpdateStatus(ByRef ListBoxName As ListBox, StatusComment As String)

        ListBoxName.Items.Add(TimeOfDay.ToString("H:mm:ss") & ": " & StatusComment)
        ListBoxName.TopIndex = ListBoxName.Items.Count - 1
    End Sub

If you don’t want to use the 24-hour time format, you can replace that bit of code with TimeOfDay.ToString(“h:mm:ss tt“) which will give you a output in this format: “7:22:14 pm”

I hope you find it useful.

Published by


I'm a guy, born and raised in Scandinavia, around 40 years old and have worked self-employed as a webmaster in lots of different fields, niches, doing all kind of work online.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.