Click here to Skip to main content
15,881,753 members
Articles / Programming Languages / Visual Basic
Article

Two-Way Progress Bar

Rate me:
Please Sign up or sign in to vote.
4.77/5 (25 votes)
6 Aug 2008CPOL4 min read 123.3K   2.4K   143   13
A Progress Bar User Control that uses a couple of intermediate level ComponentModel properties
Screenshot - SplitProgressBar.gif

Latest Update

The original control was not written to include thread-safety compliance. That has been corrected and is available in the new download(s).
Thanks to everyone for all the very kind words!

Introduction

As an application developer, it is not uncommon to run into a specific UI requirement that you cannot fulfill by using a purchased control. Often times you'll find 3rd party controls that come close but don't quite get you to your goal. Your options are to alter the UI Design for this particular requirement, or write your own control.

Background

That's the origin of this control. My application was performing a rather lengthy database update that involved a large group of parent objects and with each parent object, a large number of child-objects needed to be updated as well. The entire process would take 30 seconds to several minutes and I didn't want my users staring at an hourglass for that period of time wondering if they'd locked up.

I started out with a standard Progress Bar which was fine until I noticed several 'ticks' were so lengthy that again my concern went back to the users and not wanting to force them to sit staring at a screen that was dead for a more than a few seconds.

To solve this problem, I could have just stacked two progress bars on top of each other and updated them individually — but being a programmer, I could find no valid reason not to over-engineer this task into a single, self-contained control!

Using the Control in your Project

While in Visual Studio, right-click on the Toolbox in the Tab you wish to add the Control. Click 'Choose Items...' and navigate to where you saved the SplitProgressBar.dll. Select the DLL and click 'OK'.

This will add the control to your toolbox and you're now ready to drag it onto your forms for use.

Properties

  • BarBackColor is the background color of the panel behind the ProgressBar Cells.
  • BorderColor is color used in the lines surrounding the ProgressBar.
  • TextBackColor is the background color of the panel behind the Label.
  • TextColor is the Label's forecolor.
  • BarBottomColor and BarTopColor are the colors to use for the ProgressBar Cells.
  • BarBottom.BarMax is the upper limit that .BarValue can reach.
  • BarBottom.BarMin is the lower limit that .BarValue can reach.
  • BarBottom.BarStep tells the progress bar how to increment .BarValue every time .PerformBottomStep is pushed.
  • BarBottom.BarValue is the actual value of our ProgressBar.
  • BarTop is identical to BarBottom.
  • GradientBars if true, the control will attempt to fill in the cells using a gradient pattern.
  • SyncTopAndBottom if true, the control will ignore the BarBottom values and draw one, large solid cell using the BarTop values.
  • PanelText.TextLabel is the Text displayed in the Label Panel.
  • PanelText.TextPanelAlignment Left, Center or Right horizontal alignment used for the Label.
  • PanelText.TextPanelAutosize if true, the width of the Label Panel will be calculated based on the text.
  • PanelText.TextPanelWidth use this to adjust the size of your Label.
    *Only valid if TextPanelAutosize = False*.

Sample Usage

VB.NET
Private Sub ProcessSomething()
    ' Setup our progress bar to use ONE large cell and to paint the cells as

        gradients
    With ProgBar
        .SyncTopAndBottom = True
        .GradientBars = True
        .BarTop.BarMax = ElementsToProcess.Count
        .BarTop.BarStep = 50
        .BarTop.BarValue = 0
    End With
    Dim CurrentElement As Integer
    For Each Element As SomeElement In ElementsToProcess
        '

        ' Your code here

        '

        ' Increment our counter

        CurrentElement += 1
        ' If we're ready to update the progress bar - go do it.

        If CurrentElement Mod ProgBar.BarTop.BarStep = 0 Then
            ProgBar.PerformTopStep()
        End If
    Next
End Sub

Using both the top and bottom bars at the same time is very simple: just set .SyncTopAndBottom=False and setup your .BarBottom properties the same way you setup the .BarTop properties.

*Note*: If your control height results in cells that are smaller than 6 pixels, a solid color will be used to fill in the cells regardless of your .GradientBars value.

Cool ComponentModel Properties

Have you ever had one property alter the value of another property during design time? To get the properties window for the control to refresh the values, use the RefreshProperties to force the update like this:

VB.NET
<refreshproperties(refreshproperties.all)> _
Public Property BarMin() As Integer
    Get
        Return _Min
    End Get
    Set(ByVal value As Integer)
        If _Min <> value Then
            _Min = value
            If _Min > _Max Then Me.BarMax = _Min
            RaiseEvent BarClassChanged()
        End If
    End Set
End Property</refreshproperties(refreshproperties.all)>

Ever want to "hide" unused properties? Not all of them are "hidable" but for those that are, try this:

VB.NET
<Browsable(False)> _
Public Overrides Property Autosize() As Boolean
    Get
        Return MyBase.AutoSize
    End Get
    Set(ByVal value As Boolean)
        MyBase.AutoSize = value
    End Set
End Property

Points of Interest

This is the first control I wrote that uses expandable properties (like Size, Padding, Location, etc.) While very nice to have, figuring out how to set the properties up like this caused me a number of heartaches not the least of which is there's almost no documentation on how to create, configure or use expandable properties (that I was able to find!)

A weirdness that happens quite frequently and I have yet to figure out why: When the control is first placed on your form, click on the control and scroll the properties window down to BarBottom and look at the value displayed: "0,0,0,0" which are the default startup settings.

If you modify the Converter and rebuild the SplitProgressBar class — instead of seeing the Sub-Property values, you'll see the NAME of the class like this, "SplitProgressBar.BarClass"

At this point, you have to remove SplitProgressBar from the main project's reference list — and re-add it. Restarting Visual Studio will resolve this issue as well but since I was never intending the developer to change the sub-properties as a single line of comma-delimited text by filling in the parent property, I'm actually OK when this happens.

In fact, that's one improvement I'd like to make for the control: I would like NO text be displayed next to the parent property when collapsed. If anyone knows how to pull this off, PLEASE let me know!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
kaszur23-Aug-11 9:26
kaszur23-Aug-11 9:26 
GeneralThanks! Pin
Member 241295516-Jul-09 10:40
Member 241295516-Jul-09 10:40 
GeneralVery Nice Pin
Paul Conrad2-Aug-08 19:40
professionalPaul Conrad2-Aug-08 19:40 
GeneralRe: Very Nice Pin
Elkay3-Aug-08 8:29
Elkay3-Aug-08 8:29 
GeneralRe: Very Nice Pin
Paul Conrad3-Aug-08 18:22
professionalPaul Conrad3-Aug-08 18:22 
GeneralRe: Very Nice Pin
Elkay3-Aug-08 18:31
Elkay3-Aug-08 18:31 
GeneralRe: Very Nice Pin
Paul Conrad3-Aug-08 18:34
professionalPaul Conrad3-Aug-08 18:34 
GeneralThread safety Pin
supercat931-Jul-08 14:57
supercat931-Jul-08 14:57 
GeneralRe: Thread safety Pin
Elkay31-Jul-08 20:32
Elkay31-Jul-08 20:32 
GeneralRe: Thread safety Pin
supercat91-Aug-08 5:53
supercat91-Aug-08 5:53 
Questionhi Pin
Schatak12-Sep-07 0:52
professionalSchatak12-Sep-07 0:52 
AnswerRe: hi Pin
Elkay12-Sep-07 6:16
Elkay12-Sep-07 6:16 
AnswerRe: hi Pin
The_Mega_ZZTer31-Jul-08 11:49
The_Mega_ZZTer31-Jul-08 11:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.