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
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.
Do you want to make a Listbox in VB.net auto scroll to the last value?
I use this to log and show the progress of what my application is currently doing.
Amazingly ugly UI I made.
The first line just add date and the current time to the Listbox.
The second line is the one that actually scroll the Listbox.
The third line is refreshing the UI which might be needed if your application need to do a lot of work…
ListBox.TopIndex = ListBox.Items.Count - 1
A good idea is to use a timer to keep the Listbox scrolling so you don’t have to add the same code over and over each time you add a new item to the Listbox.
First off, I didn’t not write this code. I might have modified it but I’m not sure since I’ve had it around for quite some time.
If you’re the creator, thanks! It’s been handy. And comment and I’ll add a “credit-link” in this post. 😉
The code to get the value, text or string or whatever you choose to call it is so easy to use, short and it’s handy in many different kind of applications.
If you’re writing a Scraper in VB.net, this is a perfect Function that surely will be useful for you.
Lets say you have a string with the following content:
"Erik Johan Jonas Johanna Martin David"
And you want to pick out the name between Johanna and David:
Dim FindIt As String = GetBetween(Haystack, "Johanna", "David")
So, I used Dim in the same line to set up a string to store the text we’re picking out and then used the function called GetBetween to extract the text.
There will be spaces that you might want to use Trim on since I didn’t include the space as a prefix and suffix when using the GetBetween function.
The code is in the full post. Follow the link!
Continue reading VB.net: Get a String between 2 words(any text) inside a larger string – Supereasy!