
Introduction
It is not possible to directly put a ProgressBar or a button in a StatusBar in VB.NET. Solutions are available for C++ or VB 5 or 6.0. This is a way to do it in VB.NET. The previous version of this article, called StatusProgressBar only allowed using a ProgressBar. This new version extends the technique to any control.
Using the code
First, create a form with a StatusBar and a ProgressBar or any kind of control (tested with a button) you will put anywhere. You have to create at least a Panel in the StatusBar and make the Panels visible (.showpanels = True). The control put into the status bar will be called "child control".
Create an instance of StatusBarChild:
sbcProgressBar = New StatusBarChild(ProgressBar1, StatusBar1, 1)
The parameters are the ProgressBar (ProgressBar1), the StatusBar (StatusBar1) and the number of the statuspanel (0 for the first one on the left). An optional parameter is the margin between the statuspanel's edges and the ProgressBar. The ProgressBar is moved to the StatusBar when you create the StatusBarChild. You may put a button in the StatusBar the same way:
sbcButton= New StatusBarChild(Button1, StatusBar1, 1)
The StatusBarChild object's properties are the child control (.ChildObject), the StatusBar (.StatusBar), the panel number (.Panel) and the margin (.Margin). Its only method is .Resize, you should call it when the parent form is resized. See the demo for details.
How it works
The child control is declared as an object:
Public ChildControl As Control
The child control's parent is first changed:
ChildControl.Parent = StatusBar
Then, it must be displayed in the proper panel. Since there is no property to get the Panel's actual width, an API call is necessary. SendMessage returns a RECT value containing the coordinates of the Panel.
Private Structure RECT
Friend Left As Int32
Friend Top As Int32
Friend Right As Int32
Friend Bottom As Int32
End Structure
Dim Rectangle As RECT
SendMessage(StatusBar.Handle.ToInt32, SB_GETRECT, Panel, Rectangle)
With ProgressBar
.Left = Rectangle.Left + Margin
.Top = Rectangle.Top + Margin
.Height = Rectangle.Bottom - Rectangle.Top - 2 * Margin
.Width = Rectangle.Right - Rectangle.Left - 2 * Margin
End With
You may use this technique to put any kind of control into a StatusBar, provided it has a .parent property.
The demo uses a button in the first panel, and a ProgressBar in the second one. Click the button to make the bar progress.
History
- 1.1:
Option Strict On allowed. See the first thread below.
- 1.0: First public version. Did not allow
Option Strict On.
| You must Sign In to use this message board. |
|
|
 |
|
 |
Add a StatusBarPanel to the Status Bar, then write a thread with a timer event to set the text of that StatusBarPanel to some character, like the pipe "|".
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Thanks  KeyPointAnalyzer | 6:19 13 Aug '07 |
|
|
 |
|
 |
Nice example. I tried it using C# code and it worked great. The only issue I had was the margin parameter which didn't seem to do anything when I set it.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You can also use this method to put a control into a status bar. Saw it somewhere, cannot recall though. The following will place a progress bar into a statusbar
STEPS: 1. place the statusbar on the form (design time or runtime) 2. add panels to the statusbar and set the statusbar's showpanels to true. 3. set the style property of the panel which will host the progressbar. 4. add a progressbar to the form (runtime or design time) 5. In the load event of the form, add the progressbar to the control collection of the statusbar (e.g. statusbar1.controls.add(progressbar1) 6. In the DrawItem event of the status bar place a code like this
VB.NET 2002
Private Sub StatusBar1_DrawItem(ByVal sender As Object, ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) Handles StatusBar1.DrawItem If sbdevent.Panel Is StatusBarPanel2 Then ProgressBar1.SetBounds(sbdevent.Bounds.X, sbdevent.Bounds.Y, sbdevent.Bounds.Width, sbdevent.Bounds.Height) End If End Sub
VB.NET 2003
Private Sub StatusBar1_DrawItem(ByVal sender As Object, ByVal sbdevent As System.Windows.Forms.StatusBarDrawItemEventArgs) Handles StatusBar1.DrawItem If sbdevent.Panel Is StatusBarPanel2 Then Dim Rect As New Rectangle(sbdevent.Bounds.X , sbdevent.Bounds.Y , sbdevent.Bounds.Width , sbdevent.Bounds.Height) ProgressBar1.Bounds=Rect End If End Sub
7. That's it.
You can try the above with any control of choice. Enjoy.
-- modified at 7:18 Thursday 29th December, 2005
|
| Sign In·View Thread·PermaLink | 3.67/5 |
|
|
|
 |
|
 |
In .Net v2.0, the StatusBar control will be replaced with a StatusStrip. Inside this status strip, it will be possible to put in a status label, status panel / split button, progress bar, and a (Office 2003-style) dropdown button.
The status strip class[^].
However, for VS.NET [2003] and before, your class is still useful.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is a good thing. This kind of add-in is made to be integrated in the next version of the language.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
I was hoping to use your class and I have got a whole bunch off errors when I turn Option Strict on. They are almost all LateBinding errors with a few extras to make things fun. Can this class be modified to work with Option Strict? Thank you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Sure. I declared the child control as an object to allow any kind of control, so option strict was necessarily off. The solution is to declare it as a Control. Just change two lines:
Public ChildObject As Control ' The progressbar or any child control first line of the Class and
Public Sub New(ByRef obj As Control, ByRef sb As StatusBar, Optional ByRef intPanelNumber As Int16 = 0, Optional ByVal intMargin As Int32 = 2) the constructor. I'll correct the article soon.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|