Download Visual Basic code for this article here: visual-basic-custom-progress.zip
This article is intended for programmers studying Visual Basic programming language. It may be interesting for advanced Visual Basic programmers as well.
You will need a basic knowledge of the Visual Basic programming language and Microsoft Visual Basic 6.0 development environment installed at your computer.
Usually progress bar is used to visualize some continuous process. The user can see the total progress and psychologically waiting become less tiring and annoying for him. It is much better to see progress bar then hourglass cursor. Therefore, you need to use progress bar, if your application implements some complex or continuous calculations or data processing. Your application will look more professionally.
Standard progress bar is included in the Visual Basic distributive and it is suitable for most cases. But sometimes you may want to create your own custom progress bar. For example:
I think you can imagine your own reason when you may need custom progress bar.
A Label is one of the simplest Visual Basic controls. It is a graphical lightweight control, which requires fewer system resources than other Visual Basic controls. Lets build our progress bar using two Label controls.
Place the following Label controls at your Form:
Set the following properties for both Label controls:
You can choose other properties like Font, BackColor, ForeColor, etc. of these Label controls depending of your taste.
Our progress bar will use a range, which can be specified by Min and Max Long integer values during initialization process, and internal counter, which will be used for progress visualization. Progress bar internal counter value is represented with the Long integer type variable and can be an integer value from 1 to 2,147,483,647. An absolute value of the range cannot exceed that value.
First of all we need to create some form level variables that will keep our progress bar state during calculations:
Private m_iMin As Long Private m_iValue As Long Private m_iMaxValue As Long Private m_sWidth As Single
Now we need to create initialization code. Lets create InitProgress sub procedure. It takes two ByVal arguments that are minimal and maximal values of the progress range:
Private Sub InitProgress(ByVal iMin As Long, ByVal iMax As Long)
Save progress bar control width value and minimal range value:
m_sWidth = Me.lblBack.Width m_iMin = iMin
Calculate maximal progress bar counter value. Minimal counter value is 1.
If iMin = 1 Then m_iMaxValue = iMax Else m_iMaxValue = Abs(iMax - iMin) If iMin < 1 Then m_iMaxValue = m_iMaxValue + 1 End If End If m_iValue = 1 End Sub
We don't need any additional initialization steps.
Lets create SetProgress sub procedure, which will display current progress bar state. It takes one ByVal argument, which is a value from the range specified during the initialization. It is the current position within the range.
Private Sub SetProgress(ByVal iValue As Long)
Normalize current progress value, i.e. shift it to the diapason from 1 to m_iMaxValue:
m_iValue = Abs(iValue - m_iMin) + 1
Draw progress bar, i.e. calculate current width of the lblFace Label control depending on the current progress value:
With Me.lblFace .Width = (m_iValue * m_sWidth) / m_iMaxValue .Caption = CStr(Int(m_iValue * 100 / m_iMaxValue)) & "%" End With
Let the operating system to redraw form and to process other events:
DoEvents End Sub
That's all! Our progress bar is ready to be used. You need to call InitProgress sub one time to initialize progress bar state and then call SetProgress sub during your continuous process to show progress state.
We have created lightweight and highly customizable progress bar.
Here are some recommendations on how to use sample Visual Basic code:
Proposed progress bar implementation technique is quite flexible, so you have a lot of ways to improve your progress bar. For example:
I have used Microsoft Visual Basic 6.0 to create sample code. But as you can see we do not use any Visual Basic 6.0 specific functionality. Therefore, described code should work in any prior version of Visual Basic or Visual Basic for Application.