StatusBar control is not available in Toolbox of Visual Studio 2010. StatusStrip control replaces StatusBar in Visual Studio 2010. But for backward compatibility support, StatusBar class is available in Windows Forms. In this article, I will discuss how to create and use a StatusBar using StatusBar class in a Windows Forms application.
A StatusBar control is a combination of StatusBar panels where each panel can be used to display different information. For example, one panel can display current application status and other can display date and other information and so on. A typical StatusBar sits at the bottom of a form.
I highly recommend using StatusStrip control if you are working with Visual Studio 2010.
Creating a StatusBar
StatusBar class represents a StatusBar.
Dim mainStatusBar As New StatusBar()
A StatusBar is a combination of StatusBar panels. StatusBarPanel class represents a StatusBar panel. The following code snippet creates two panels and adds them to the StatusBar.
Dim statusPanel As New StatusBarPanel()
Dim datetimePanel As New StatusBarPanel()
statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken
statusPanel.Text = "Application started. No action yet."
statusPanel.ToolTipText = "Last Activity"
statusPanel.AutoSize = StatusBarPanelAutoSize.Spring
mainStatusBar.Panels.Add(statusPanel)
datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised
datetimePanel.ToolTipText = "DateTime: " + System.DateTime.Today.ToString()
datetimePanel.Text = System.DateTime.Today.ToLongDateString()
datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents
mainStatusBar.Panels.Add(datetimePanel)
Now, make sure ShowPanels property is true.
mainStatusBar.ShowPanels = True
In the end, we add StatusBar to the Form.
Controls.Add(mainStatusBar)
Now let's create a Windows Forms application with a few controls on it. We are going to show current activity and date on the status bar. The Form looks like following.
Here is the complete code. Download attached project for more details.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateDynamicStatusBar()
End Sub
Dim mainStatusBar As New StatusBar()
Dim statusPanel As New StatusBarPanel()
Dim datetimePanel As New StatusBarPanel()
Private Sub CreateDynamicStatusBar()
statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken
statusPanel.Text = "Application started. No action yet."
statusPanel.ToolTipText = "Last Activity"
statusPanel.AutoSize = StatusBarPanelAutoSize.Spring
mainStatusBar.Panels.Add(statusPanel)
datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised
datetimePanel.ToolTipText = "DateTime: " + System.DateTime.Today.ToString()
datetimePanel.Text = System.DateTime.Today.ToLongDateString()
datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents
mainStatusBar.Panels.Add(datetimePanel)
mainStatusBar.ShowPanels = True
Controls.Add(mainStatusBar)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
statusPanel.Text = "Button is clicked."
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles TextBox1.TextChanged
statusPanel.Text = "TextBox edited."
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged
statusPanel.Text = "CheckBox is checked."
End Sub
End Class
Summary
In this article, we discussed discuss how to create and use a StatusBar control in a Windows Forms application.