Status Bar
A status bar is a horizontal bar that displays at the bottom of the form, for the purpose of providing information to the user regarding the state of one or more aspects of the application. In this demo program, the status bar displays a general message in the first “panel”, followed by a display of the status of the Caps Lock, Num Lock, Scroll Lock, and Insert keys respectively, followed by a display of the current time and date respectively. (Note: I use the word “panel” to describe these areas, as that was the term used in classic VB (VB6 and before). In this demo however, we are using the StatusStrip control, and these “panel” areas are simply label objects.)
As shown in the screen shot above, the general message says “Ready.” However, when you hover the mouse over the “Reports” button, the message will change to “Run a report.” Similar messages will appear in that area when you hover over the Utilities and Exit buttons as well. |
For Caps Lock, Num Lock, and Scroll Lock, the corresponding status areas of the StatusStrip will display in normal text or grayed out text depending on the “on” or “off” status of those keys, respectively. |
For the Insert key, the status area will read “INS” or “OVR” as that key is toggled on or off (“INS” means the keyboard is in “insert” mode; “OVR” means it is in “overwrite” mode). |
|
|
|
To build the StatusBar Demo sample project, perform the following steps:
Assuming you have started a new Windows Forms VB project, change the Text of the default form to “StatusBar Demo”. Name the form “frmStatusBarDemo”. Set the KeyPreview property to True.
|
|
To add a status bar for use with your VB program, go to the “Menus & Toolbars” section of the toolbox and double-click the StatusStrip control. It will then appear in the component tray below the form, as well as along the bottom of the form, where you can begin to add the status items. It starts you off with a drop-down list to select an item to add to the status.
|
|
For the purpose of the demo program, we are concerned only with the StatusLabel item. |
|
As with the MenuStrip and ToolStrip controls presented in previous tutorials, you can also manage items by going to the smart tag menu on the StatusStrip and clicking “Edit Items…” to bring up the Item Collection Editor. |
|
Using either method or a combination of the two, the goal is to place seven StatusLabels on the StatusStrip, named as shown in the screen shot on the right. Also, for proper visual appearance, set the BorderStyle for all seven items to “SunkenOuter”, and set the BorderSides property for all seven items to “All” |
|
Other specific properties that should be set are: For sbrpnlMainText, set the Spring property to True. This will cause that label to expand in size so that it fills up the remaining space along the width of the status strip (all other items will take up only the space they need to show whatever text they are going to show).
For sbrpnlCapsLock, sbrpnlNumLock, sbrpnlScrollLock, and sbrpnlInsMode, set the Text property to “CAPS”, “NUM”, “SCRL”, and “INS” respectively. |
|
After the StatusStrip’s items have been set, add a Timer to the form. Name the timer “tmrTime”, set its Enabled property to True and set its Interval property to 60000 (so that it will fire once per minute). |
|
Add three buttons to the form, named “btnReports”, “btnUtlities”, and “btnExit”, placed as shown in the screen shot on the right. |
|
At this point, the coding can be begin. In the Load event for the form, the text for the main message label is set to “Ready.”; the Enabled property of the CapsLock, NumLock, and ScrollLock indicator labels are set to the corresponding Boolean property of the My.Computer.Keyboard object; the insert/overwrite indicator item is set to “INS”; and the SetShowDateTime is called (code for the SetShowDateTime Sub is shown below). |
Private Sub frmStatusBarDemo_Load(ByVal eventSender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load
sbrpnlMainText.Text = "Ready."
sbrpnlCapsLock.Enabled = My.Computer.Keyboard.CapsLock sbrpnlNumLock.Enabled = My.Computer.Keyboard.NumLock sbrpnlScrollLock.Enabled = My.Computer.Keyboard.ScrollLock sbrpnlInsMode.Text = "INS"
SetShowDateTime()
End Sub
|
The SetShowDateTime Sub gets the current date and time from the system (via Date.Now) and formats the date and time accordingly for display in the designated StatusStrip items. |
Private Sub SetShowDateTime() Dim dtmNow As Date = Date.Now sbrpnlDate.Text = FormatDateTime(dtmNow, DateFormat.ShortDate) sbrpnlTime.Text = Format(dtmNow, "h:mm tt") End Sub
|
The SetShowDateTime Sub is also called from the timer’s Tick event, which is fired every minute. |
Private Sub tmrTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTime.Tick SetShowDateTime() End Sub
|
To detect when the user has pressed the CapsLock, NumLock, ScrollLock, or Insert key, the form’s KeyDown event is used. (Note: This event will only fire if the form’s KeyPreview property has been set to True.) To determine which of the four keys of interest has been pressed, e.KeyCode is tested. For CapsLock, NumLock, and ScrollLock, the Enabled property of StatusStrip indicator item is toggled to the opposite of whatever it was before (if True set False, if False set to True). For the Insert/Overwrite indicator item, the text is toggled between “INS” and “OVR”. |
Private Sub frmStatusBarDemo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _ Handles Me.KeyDown
Select Case e.KeyCode Case Keys.CapsLock sbrpnlCapsLock.Enabled = Not sbrpnlCapsLock.Enabled Case Keys.NumLock sbrpnlNumLock.Enabled = Not sbrpnlNumLock.Enabled Case Keys.Scroll sbrpnlScrollLock.Enabled = Not sbrpnlScrollLock.Enabled Case Keys.Insert sbrpnlInsMode.Text = IIf(sbrpnlInsMode.Text = "OVR", "INS", "OVR") End Select
End Sub
|
Here we have coded a MouseHover event handler for the three buttons on the form. This code sets the text for the main text label of the StatusStrip item, based on which button the mouse is hovering over. |
Private Sub Button_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnReports.MouseHover, _ btnUtilities.MouseHover, _ btnExit.MouseHover
Dim objBtn As Button = DirectCast(sender, Button)
Select Case objBtn.Name Case "btnReports" sbrpnlMainText.Text = "Run a report." Case "btnUtilities" sbrpnlMainText.Text = "Perform a utility." Case Else sbrpnlMainText.Text = "Exit this program." End Select
End Sub
|
Similarly, a MouseLeave event handler is coded for the three buttons. When the mouse moves off of any of these buttons, the main text is set back to “Ready.” |
Private Sub Button_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnReports.MouseLeave, _ btnUtilities.MouseLeave, _ btnExit.MouseLeave
sbrpnlMainText.Text = "Ready."
End Sub
|
The Click event for the Exit button invokes the Close event for the form, which ends the program. |
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub
|
Download the project files for this sample application here.