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!