A Prize Console-ation in VB 2005
The ConsoleProgressBar Class
I created the ConsoleProgressBar class to handle the details of initializing and updating the progress bar. The constructor takes one parameter, called MaximumValue:
Public Sub New(ByVal MaximumValue As Long) m_length = Console.WindowWidth - 10 m_left = 7 m_right = m_left + m_length + 1 m_progressBarRow = 1 m_messageBarRow = m_progressBarRow + 1 m_percentPosition = 4 m_maximumValue = MaximumValue m_currentValue = 0 Initialize() End Sub
I hard-coded some of the values and computed others based on the hard-coded values. I made the class simple for this article, but you could extend the constructor to take other values as parameters to make the progress bar more configurable.
The Progress Bar Progresses
Nothing else happens with the ConsoleProgressBar object until the Update method is invoked:
Public Sub Update(ByVal CurrentValue As Long) m_currentValue = CurrentValue m_currentBarLength = CInt((m_currentValue / m_maximumValue) _ * m_length) Refresh() End Sub
The Update method takes one value as a parameter, which in this case will be the current file number just copied. I set the member variable m_currentValue and then computed m_currentBarLength. The result of the computation gives the number of columns the progress bar should currently cover.
Finally, I call the Refresh method, which in turn calls the UpdatePercentComplete, UpdateProgressBar, and UpdateMessageBar methods.
Because all three methods are similar in function, I will focus on the UpdateProgressBar method:
Private Sub UpdateProgressBar() Dim originalForegroundColor As ConsoleColor = _ Console.ForegroundColor Dim originalBackgroundColor As ConsoleColor = _ Console .BackgroundColor Console.ForegroundColor = ConsoleColor.Black Console.BackgroundColor = ConsoleColor.Green Console.SetCursorPosition(m_left + 1 m_progressBarRow) Dim progress As New String("O", m_currentBarLength) Console.Write(progress) Console.ForegroundColor =originalForegroundColor Console.BackgroundColor = originalBackgroundColor End Sub
Once again, the first thing the code does is save the current foreground and background colors. It then sets the ForegroundColor property to Black and the BackgroundColor property to Green. After positioning the cursor at the left edge of the progress bar, it prints a string of Os with a length of m_currentBarLength.
After so much description, you must be eager to see the application run (see Figures 2 and 3) and check out the results (see Figure 4). I've set up a directory with a bunch of dummy files to test copying. I recommend you do the same before you use DirCopy to copy any critical files.
Figure 2: Starting DirCopy
Figure 3: DirCopy Running
Figure 4: DirCopy Finished
The DirCopy application, while potentially useful, is not quite production ready. To make it more robust, I would need to add lots more error handling (in other words, the boring, tedious stuff). You also could improve the ConsoleProgressBar class to allow more control. The following are some more possible enhancements:
- Allow more control of the placement and length of the bar
- Allow the percent complete area to be placed anywhere the programmer chooses
- Allow custom messages in the message bar
- Add an option to choose between a horizontal or vertical progress bar
More to Explore
I hope that this article has been useful in showing you some of the cool new console application features available in VB 2005. Although the features I covered are some of my favorites, I urge you to check the numerous other new console app features to find a few favorites of your own.
Download the Code
To download the accompanying source code for DirCopy, click here.
About the Author
Josh Fitzgerald is an applications development group leader for a large medical device company in Warsaw, Indiana. Designing and developing Visual Basic .NET applications is only one of his responsibilities, but it is his favorite part of his job. You can reach Josh at firstname.lastname@example.org.
Page 2 of 2