Change Color and Style of Progress bar in VB6.0 and VB.Net

Whenever we run an application with Loop process in VB6.0 or VB.Net and require a progress steps for showing Progress bar in Vb6.0. In that process, we need to demonstrate the running circle process inside that application. At that point we use Progressbar.

Fundamentally Progress Bar is that control in which we portray the Loop procedure. So, here SKOTechlearn will describe the easy way to change Color of Progress bar in VB and Style of Progress bar in VB6.0 and VB.Net.

Use Of Chart Control with Graph Design in Vb6.0

Here we will understand with two way, but the base is same. There will be the description of two Application tools process:

  1. Change ProgressBar Style in Vb6.0
  2. Change ProgressBar Style in VB.net

ProgressBar in VB6.0 and VB.Net
Now Let’s Learn:

How to Change Progress barStyle and Use of Progress bar in VB6.0?



1. Change Progress Bar Style in Vb6.0:

First we need to find out about Controls, Properties and use. For including this control in VB6.0 Form, you need to find component with given steps:

Project (menu)>> component>> Controls(Tab)>> Microsoft Windows Common Controls 6.0 (SP6)

After that choose this control and drag it to Form. Now come to "Properties" window and select some properties to change predefined style of this control.

 ProgressBar Scrolling (Bar Style) Options
 0 - ccScrollingStandard
 1 - ccScrollingSmooth

Scrolling Properties ProgressBar
Scrolling Properties
There is another property, where you can change orientation.

 Progressbar Orientation options
 0 - ccOrientationHorizontal
 1 - ccOrientationVertical

How to change ProgressBar color in vb6.0?


Now come to some interesting features of it for changing the color. So, first you have to know that there is no properties setting for this process, you have to write code just as mention bellow.
Private Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
 
Private Const PrgBrClr As Long = &H409  'change Bar Color
Private Const PrgBackClr As Long = &H2001  'Change Background Color

Private Sub StrtPrbr_Btn_Click() 'call function inside Button’s events Call SendMessageLong(MyPrgBar1.hwnd, PrgBrClr, ByVal 215, ByVal 205) 'call function for Bar color change Call SendMessageLong(MyPrgBar1.hwnd, PrgBackClr, ByVal 0&, ByVal 55) 'call function for Back color change 'create a loop process MyPrgBar1.Max = 10000 MyPrgBar1.Value = 0 Dim ik As Integer ik = 0 While ik < 10000 MyPrgBar1.Value = MyPrgBar1.Value + 1 DoEvents ik = ik + 1 Wend MsgBox "Done" End Sub

How to define ProgressBar in Loop in VB6.0?


You can see that above given code process, there is also a loop in which this control is characterize inside it. Every time you have to increment value of MyPrgBar1 inside "While Loop".

When you run Form and press “StrtPrbr_Btn”, it will show you as following image.
ProgressBar Process with Color
ProgressBar Color

Like blinking look, you have to just modify and add some coding inside loop. You will find a different type of attractive process just mention bellow.
Dim ik, sk As Integer
ik = 0
sk = 5
  While ik < 10000
     MyPrgBar1.Value = MyPrgBar1.Value + 1
     If ik Mod 200 = 0 Then 
        'Color will Change everytime inside RGB() 
        Call SendMessageLong(MyPrgBar1.hwnd, PgrBrClr, ByVal 0&, ByVal RGB(sk, Rnd, Rnd + 50))
        Call SendMessageLong(MyPrgBar1.hwnd, PrgBackClr, ByVal 0&, ByVal RGB(120, sk, 155))
        sk = sk + 15
     End If
     If sk > 200 Then
        sk = 5
     End If
     DoEvents
     ik = ik + 1
  Wend
Color_Change_in_Loop
Color change during loop

How To Change and Use of Progress bar in VB.Net?


2. Change ProgressBar Style in VB.Net:

In vb.net, you can see the advance feature to graphically introduce controls with different style. So, First we understand about Properties.

ProgressBar Properties Setting:


ProgressBar “Scrolling” in VB.Net work same as VB6.0, But you will find options' differences.

 Style (Bar style Options)
 Blocks
 Continuous
 Marquee

How to change bar color in Progressbar in VB?


If you want to change bar color of Progress Bar, you have to write code as mention bellow:

First you have to globally define Function:
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Enum PrgBrColor
    ClrGreen = &H1
    ClrRed = &H2
    ClrYellow = &H3
    
End Enum

Now Create a class and call this function inside class:
Private Shared Sub ChangePrbClr(ByVal Prgnm As System.Windows.Forms.ProgressBar, ByVal Prgclr As PrgBrColor)
    SendMessage(Prgnm.Handle, &H410, Prgclr, 0)
End Sub  

After that call this class on event where you perform action:
Private Sub PrgBr_BtnClick(sender As Object, e As EventArgs)

  ChangePrbClr(MyPrgBr1, PrgBrColor.CLrRed)
End Sub

Trick to Photo Crop in VB6.0 with Moving and Drawing Rectangle.

If you want to change the look like Blinking Progress bar during loop, there is following code which show the blinking style:
Private Sub PrgBr_BtnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrgBr_Btn.Click
        MyPrgBr1.Maximum=10012   'define Maximum value
        MyPrgBr1.Value=0
        Dim ik As Double
        While  ik<10012 
            MyPrgBr1.Value=MyPrgBr1.Value+1
            label1.Text=MyPrgBr1.Value
            If ik Mod 2=0 Then
                ChangePrbClr(MyPrgBr1, PrgBrColor.ClrYellow)
            End If
            If ik Mod 3=0 Then
                ChangePrbClr(MyPrgBr1, PrgBrColor.CLrRed)
            End If
            If ik Mod 7=0 Then
                ChangePrbClr(MyPrgBr1, PrgBrColor.ClrGreen)
            End If
            UpdatePBar()
            ik=ik+1
            Application.DoEvents
        End While
        MsgBox("Done", MsgBoxStyle.Information)
    End Sub

Note : Loop value can not be greater than Maximum Value.


How to show percentage in ProgressBar in VB?


If you want to show percentage like professional look, just write code inside a custom class.
Private Sub PercentInBar()

        Dim PGB_X As Single
        Dim PGB_Y As Single
        Dim percent As String = CType((MyPrgBr1.Value / MyPrgBr1.Maximum * 100), Integer).ToString & "%"
        Dim grph As Graphics = MyPrgBr1.CreateGraphics

        Dim psize As SizeF = grph.MeasureString(percent, MyPrgBr1.Font, MyPrgBr1.Width)

        PGB_X = (MyPrgBr1.Width / 2) - (psize.Width / 2)
        PGB_Y = (MyPrgBr1.Height / 2) - (psize.Height / 2)

        grph.DrawString(percent, MyPrgBr1.Font, Brushes.Black, PGB_X, PGB_Y)

    End Sub

After that call this class in “PercentInBar()” button's "Click"event inside loop same as mention bellow.
Private Sub PrgBr_BtnClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrgBr_Btn.Click

        MyPrgBr1.Maximum = 10012
        MyPrgBr1.Value = 0
        Dim i As Double
        While  i < 10012
            MyPrgBr1.Value = MyPrgBr1.Value + 1
            label1.Text = MyPrgBr1.Value
            PercentInBar()
            i = i+1
            Application.DoEvents
        End While
        MsgBox("Done", MsgBoxStyle.Information)
    End Sub

In this way the running application will looks like bellow.
ProgressBar_Process_Style_Percentage

So, there are some easiest way to Change Color and Style of Progress Bar in VB6.0 and VB.Net with SKOTechLearn Tips.

Also Learn Some Visual Basic Programming Tips:

Easy Learning Steps to Add ADODC control in VB6 for Database Connection

Use Adodc Control for Database Connection with Login Form in VB6

0 comments: