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

A class to put a ProgressBar or any control into a StatusBar

Rate me:
Please Sign up or sign in to vote.
4.76/5 (29 votes)
18 May 2005CPOL2 min read 106.9K   1.8K   75   8
How to simply display a control inside the StatusBar of your program.

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:

VB
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:

VB
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:

VB
Public ChildControl As Control

The child control's parent is first changed:

VB
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.

VB
Private Structure RECT
  Friend Left As Int32
  Friend Top As Int32
  Friend Right As Int32
  Friend Bottom As Int32
End Structure
VB
Dim Rectangle As RECT
' Use the API to get the panel's display rectangle
SendMessage(StatusBar.Handle.ToInt32, SB_GETRECT, Panel, Rectangle)
VB
' Resize
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.

License

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


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

Comments and Discussions

 
GeneralA really easy way Pin
Ryan Danforth10-Apr-08 16:54
Ryan Danforth10-Apr-08 16:54 
GeneralThanks Pin
KeyPointAnalyzer13-Aug-07 5:19
KeyPointAnalyzer13-Aug-07 5:19 
GeneralThanks Pin
RanvA23-Apr-06 9:34
RanvA23-Apr-06 9:34 
GeneralAn alternative method Pin
kaGyamfi29-Dec-05 1:17
kaGyamfi29-Dec-05 1:17 
GeneralIn .Net v2.0 you will be able to do this. Pin
oshah16-May-05 2:19
oshah16-May-05 2:19 
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.
GeneralRe: In .Net v2.0 you will be able to do this. Pin
Eric Marcon17-May-05 2:12
Eric Marcon17-May-05 2:12 
GeneralErrors when Option Strict is on Pin
webbee9-Feb-05 10:26
webbee9-Feb-05 10:26 
GeneralRe: Errors when Option Strict is on Pin
Eric Marcon11-Feb-05 7:57
Eric Marcon11-Feb-05 7:57 

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.