StatusBar In C#

In recent versions of .NET, the StatusBar control has been replaced with the StatusStrip control. To implement Status Bar in C# and Windows Forms app, please visit StatusStrip Control In C#.

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

Creating a StatusBar

StatusBar class represents a StatusBar.
  1. StatusBar mainStatusBar = 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.
  1. StatusBarPanel statusPanel = new StatusBarPanel();  
  2. StatusBarPanel datetimePanel = new StatusBarPanel();  
  3.   
  4. // Set first panel properties and add to StatusBar  
  5. statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken;  
  6. statusPanel.Text = "Application started. No action yet.";  
  7. statusPanel.ToolTipText = "Last Activity";  
  8. statusPanel.AutoSize = StatusBarPanelAutoSize.Spring;  
  9. mainStatusBar.Panels.Add(statusPanel);  
  10.   
  11. // Set second panel properties and add to StatusBar  
  12.   
  13. datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised;  
  14. datetimePanel.ToolTipText = "DateTime: " + System.DateTime.Today.ToString();  
  15.   
  16. datetimePanel.Text = System.DateTime.Today.ToLongDateString();  
  17. datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents;  
  18. mainStatusBar.Panels.Add(datetimePanel); 

Now, make sure ShowPanels property is true. 
  1. mainStatusBar.ShowPanels = true;  
In the end, we add StatusBar to the Form.
  1. 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.

StatusBarImg1.jpg 

Here is the complete code. Download attached project for more details.

  1. protected StatusBar mainStatusBar = new StatusBar();  
  2. protected StatusBarPanel statusPanel = new StatusBarPanel();  
  3. protected StatusBarPanel datetimePanel = new StatusBarPanel();   
  4.   
  5. private void CreateStatusBar()  
  6. {  
  7.     // Set first panel properties and add to StatusBar  
  8.     statusPanel.BorderStyle = StatusBarPanelBorderStyle.Sunken;  
  9.     statusPanel.Text = "Application started. No action yet.";  
  10.     statusPanel.ToolTipText = "Last Activity";  
  11.     statusPanel.AutoSize = StatusBarPanelAutoSize.Spring;  
  12.     mainStatusBar.Panels.Add(statusPanel);   
  13.   
  14.     // Set second panel properties and add to StatusBar  
  15.     datetimePanel.BorderStyle = StatusBarPanelBorderStyle.Raised;  
  16.   
  17.     datetimePanel.ToolTipText = "DateTime: " + System.DateTime.Today.ToString();  
  18.   
  19.     datetimePanel.Text = System.DateTime.Today.ToLongDateString();  
  20.     datetimePanel.AutoSize = StatusBarPanelAutoSize.Contents;  
  21.     mainStatusBar.Panels.Add(datetimePanel);   
  22.     mainStatusBar.ShowPanels = true;  
  23.     // Add StatusBar to Form controls  
  24.     this.Controls.Add(mainStatusBar);  
  25. }   
  26.   
  27. private void button1_Click(object sender, EventArgs e)  
  28. {  
  29.     statusPanel.Text = "Button is clicked.";  
  30. }   
  31.   
  32. private void checkBox1_CheckedChanged(object sender, EventArgs e)  
  33. {  
  34.     statusPanel.Text = "CheckBox is checked.";  
  35. }   
  36.   
  37. private void textBox1_TextChanged(object sender, EventArgs e)  
  38. {  
  39.     statusPanel.Text = "TextBox edited.";  
  40. }  
Summary

In this article, we discussed discuss how to create and use a StatusBar control in a Windows Forms application.

Further readings 


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.