Click here to Skip to main content
Click here to Skip to main content

Embed a Progressbar in your Statusbar

, 24 Feb 2004
Rate this:
Please Sign up or sign in to vote.
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.

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.

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.

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.

    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.

    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

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.

    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

Share

About the Author

Matthew Hazlett
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

 
QuestionChange forecolor PinmemberJames L26-Nov-06 10:26 
GeneralLove it! thanks Pinmembertrend42112-Jun-06 10:40 
GeneralMore Flexible Status Bar PinmemberBob 500013-Dec-05 5:43 
GeneralRe: More Flexible Status Bar PinmemberGreenShoes19-Apr-07 0:12 
GeneralThanks for the code PinmemberSeth O'Neal12-May-05 11:00 
GeneralActivate and Deactivate ProgressBar Pinmemberhuhn30-Dec-04 0:35 
Generalnetscape animated progressbar Pinmemberthomasdev27-Oct-04 22:58 
GeneralSlight change to make the border "better" PinsussAnonymous22-Sep-04 17:01 
GeneralC# version ... modified Pinmemberschmidro12-Jul-04 11:08 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150301.1 | Last Updated 25 Feb 2004
Article Copyright 2004 by Matthew Hazlett
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid