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

Embed a Progressbar in your Statusbar

Rate me:
Please Sign up or sign in to vote.
4.38/5 (25 votes)
24 Feb 20042 min read 141.3K   2.2K   73   10
Simple subclassed StatusBar


Introduction

This is a relatively short article about embedding a progressbar in the statusbar. I have read many different implementations of this idea but they are usually to complicated for such an easy task. I will explain how I do it with a minimum of coding effort.

Sub Classing

Sub Classing is the key to most every custom component. A sub classed control is merely a class that inherits from an existing control. Three is already a perfectly good statusbar control and already a perfectly good progressbar all we have to do is combined the two. So lets start by creating a class that inherits from statusbar.

VB.NET
Public Class ProgressStatus : Inherits StatusBar
End Class

Just by doing this you have created a copy of the statusbar control.

Adding the ProgressBar

Now that we have a control that behaves exactly like the statusbar we can add other control to our new statusbar.

VB.NET
Public Class ProgressStatus : Inherits StatusBar
    Public progressBar As New progressBar    

      
    Sub New()
        progressBar.Hide()

        Me.Controls.Add(progressBar)
    End Sub
End Class

Now we have created a progressbar in out statusbar control. Now all that remains to be done is position the progressbar in the correct place.

Positioning the new control

For this we need a variable that will specify the panel where the statusbar is positioned, so lets create one.

VB.NET
Public Class ProgressStatus : Inherits StatusBar
    Public progressBar As New progressBar
    Private _progressBar As Integer = -1    

  
    Sub New()
        progressBar.Hide()

        Me.Controls.Add(progressBar)
    End Sub

Now we need a property to set the position of the progressbar.

VB.NET
Public Property setProgressBar() As Integer
    Get
        Return _progressBar
    End Get
    Set(ByVal Value As Integer)
        _progressBar = Value
        Me.Panels(_progressBar).Style = StatusBarPanelStyle.OwnerDraw
    End Set
End Property

What we do here is set the style of the panel you send the property to OwnerDraw. We do this so we can access the X and Y information of where the panel is located. Now all that we have to do is create the OwnerDraw handler and tell it to position our progressbar there.

VB.NET
    Private Sub Reposition(ByVal sender As Object, _
      ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) _
      Handles MyBase.DrawItem

        progressBar.Location = New Point(sbdevent.Bounds.X, _
           sbdevent.Bounds.Y)
        progressBar.Size = New Size(sbdevent.Bounds.Width, _
           sbdevent.Bounds.Height)
        progressBar.Show()
    End Sub
End Class

As you can see we never actually draw anything all we do is reposition the progressbar. We are all done!

Completed Code

VB.NET
Public Class ProgressStatus : Inherits StatusBar
    Public progressBar As New progressBar
    Private _progressBar As Integer = -1


    Sub New()
        progressBar.Hide()

        Me.Controls.Add(progressBar)
    End Sub


    Public Property setProgressBar() As Integer
        Get
            Return _progressBar
        End Get
        Set(ByVal Value As Integer)
            _progressBar = Value
            Me.Panels(_progressBar).Style = StatusBarPanelStyle.OwnerDraw
        End Set
    End Property


    Private Sub Reposition(ByVal sender As Object, _
         ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) _
         Handles MyBase.DrawItem
        progressBar.Location = New Point(sbdevent.Bounds.X, _
           sbdevent.Bounds.Y)
        progressBar.Size = New Size(sbdevent.Bounds.Width, _
           sbdevent.Bounds.Height)
        progressBar.Show()
    End Sub
End Class

Using this control

Adding it to your project is easy, for easy access we will put in in its own sub.

VB.NET
Public StatusBar As New ProgressStatus


Private Sub InitializeStatusBar()
    Dim info = New System.Windows.Forms.StatusBarPanel
    Dim progress = New System.Windows.Forms.StatusBarPanel


    info.Text = "Ready"
    info.Width = 100

    progress.AutoSize = _
       System.Windows.Forms.StatusBarPanelAutoSize.Spring

    With StatusBar
        .Panels.Add(info)
        .Panels.Add(progress)
        .ShowPanels = True
        .setProgressBar = 1
        .progressBar.Minimum = 0
        .progressBar.Maximum = 100
    End With

    Me.Controls.Add(StatusBar)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
    InitializeStatusBar()
    StatusBar.progressBar.Value = 50
End Sub

Thats it!, enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I started programming for fun when I was about 10 on an Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)

The way I learned was by example, now its time to give back to the next generation of coders.



Comments and Discussions

 
QuestionNice one!! Pin
Siddharth R Barman14-Nov-21 0:28
Siddharth R Barman14-Nov-21 0:28 
QuestionChange forecolor Pin
James L26-Nov-06 9:26
James L26-Nov-06 9:26 
This is a great control. Does anyone know how to change the forecolor and backcolor?

Jim
GeneralLove it! thanks Pin
trend42112-Jun-06 9:40
trend42112-Jun-06 9:40 
GeneralMore Flexible Status Bar Pin
Bob 500013-Dec-05 4:43
Bob 500013-Dec-05 4:43 
GeneralRe: More Flexible Status Bar Pin
GreenShoes18-Apr-07 23:12
GreenShoes18-Apr-07 23:12 
GeneralThanks for the code Pin
Seth O'Neal12-May-05 10:00
Seth O'Neal12-May-05 10:00 
GeneralActivate and Deactivate ProgressBar Pin
huhn29-Dec-04 23:35
huhn29-Dec-04 23:35 
Generalnetscape animated progressbar Pin
thomasdev27-Oct-04 21:58
thomasdev27-Oct-04 21:58 
GeneralSlight change to make the border "better" Pin
Anonymous22-Sep-04 16:01
Anonymous22-Sep-04 16:01 
GeneralC# version ... modified Pin
schmidro12-Jul-04 10:08
schmidro12-Jul-04 10:08 

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.