ConsoleProgressBar - Simple Progress Bar Function for your VB.Net Console Application

Posted by Daniel - 1,366 Views

progress bar for console applicationIt is good to know that long process is still ticking and one of the best way to provide the indicator is to have a progress bar. I am pretty sure you already knew how to display progress bar when you deal with windows form application but what about console application, is there any progress bar component which can be easily incorporated into the console screen?

I’ve searched the internet to find this kind of stuff but I found nothing (let me know if I am wrong), so there is no other way than to create it myself and here it is: my own progress bar for console application written in VB.Net.

It’s very simple. I use a single sub procedure instead of packing it as a class since I don’t think it’s required. A sub procedure called RenderProgressBar is the only procedure responsible to process and render the progress bar on the console screen.

progressbar.jpgThere are two options you can use with this function. The first one is to display the progress bar with maximum limit specified. With this option the percentage of process and the “bar” itself will be displayed. The second one is displaying the progress without specifying the max limit so there is no “bar” with this option, it will only display the iteration value - it’s good when you have no idea when a process will be completed but you still want to show that the process is still ticking.

Following is the RenderProgressBar code:

Sub RenderProgressBar(ByVal intMaxValue As Integer, ByVal intProgress As Integer, ByVal intLeftPos As Integer, ByVal intTopPos As Integer)
Dim strResult As String
Dim intPercent As Integer

If (intMaxValue > 0) Then
intPercent = Math.Round((intProgress / intMaxValue) * 100, 0)
If intPercent >= 10 Then
strResult = “| “ & intPercent & “% |” & StrDup(intPercent \ 10, “=”) & StrDup(10 - (intPercent \ 10), ” “) & “|”
Else
strResult = “| “ & intPercent & “% |” & StrDup(10, ” “) & “|”
End If
Else
intPercent = intProgress
strResult = “| “ & FormatNumber(intPercent, 0) & ” |”
End If
Console.CursorVisible = False
Console.SetCursorPosition(intLeftPos, intTopPos)
Console.Write(strResult)
Console.CursorVisible = True
End Sub

The demo project along with the RenderProgressBar sub procedure is included in this post. Feel free to enhance this function as you wish, in fact I encourage you to do it. You may pack it as a class or anything as long as it gets better. Good luck :)

Available Download:

__________________________

Related posts brought to you by Yet Another Related Posts Plugin.

share this article

Digg del.icio.us Netscape StumbleUpon Yahoo! MyWeb reddit Furl Magnolia Newsvine Technorati SlashDot Blinklist Simpy Google
This post as PDFPosted in: Programming - March 2008

One Response to “ConsoleProgressBar - Simple Progress Bar Function for your VB.Net Console Application”

  1. JohnnyIdol Says:

    Nice tip, nice snippet. Console apps rock!

    JohnnyIdol’s last blog post: [C++] How to disable Alt+Tab (and other key combinations)



Leave a Reply


Options for your comment:





Get my Full Feed Here or you can subscribe to one of my category based feeds below:
Coffee Break

Latest Blog Entries

Categories

Neighbours and Friends

Login Here

Comments - Thanks Guys :)

  • Coarveadvexon: As the call, so the echo :D Coarveadvexon’s last blog post: Ford Kuga 2.5 Turbo
  • Chat: nice thank you very much for this article.
  • a_suliman: thanx for every thing
  • Miss Lunatic: Jaja, it’s a yolk. Thanks for the compliment ;) Miss Lunatic’s last blog post: Pitas y guacamole
  • Miss Lunatic: Thank you! Your style css is awesome. I’m going to start customizing with it. Thank you :D Miss Lunatic’s last blog...