Windows Forms StatusStrip Control
A typical status bar control, placed on the bottom of a Form is used to display some text that represents a status of the application and user actions. In the previous versions of the Windows Forms, the StatusBar control is used to provide the status bar functionality. In Windows Forms 4.0 that is a part of Visual Studio 2010, the StatusStrip control replaces the StatusBar control. The StatusStrip control not only provides status bar functionality but also provides features to add rich user interfaces to a status bar such as a ProgressBar, DropDownButton and SplitButton controls.
In this article, we will learn how to build status bar enabled Windows applications using Visual Studio 2010.
Similar to any other Windows Forms control, we can use either use design-time approach or runtime approach to create a StatusStrip control. The design-time approach is used in the most of the cases but there will be times when we may need the run-time approach.
Note
In this tutorial, I use the design-time approach as the default approach but I will also show the run-time approach side by side.
Creating a StatusStrip
To build a StatusStrip control at design-time, simply drag and drop a StatusStrip control from the Toolbox to the Form. The default StatusStrip control is docked at the bottom of the Form as shown in Figure 1.
Figure 1
The designer also allows you to add several controls to a StatusStrip control including a StatusLabel, ProgressBar, DropDownButton, and a SplitButton. If you click on little dropdown icon, you will see four of these controls in the listing as shown in Figure 2. As soon as you select one of these controls, it will be added to the StatusStrip control.
Figure 2
Run-time
In Windows Forms, the StatusStrip class represents the StatusStrip control. Similar to any other Windows control, we create a class object, set its properties, methods and events and add the control to the Form's controls.
The code snippet in Listing 1 creates a StatusStrip control. Once a control is created, we need to set its properties and call Form.Controls.Add method to add the StatusStrip control to a Form's controls.
- StatusStrip dynamicStatusStrip = new System.Windows.Forms.StatusStrip();
-
-
-
-
- Controls.Add(dynamicStatusStrip);
Listing 1
StatusStrip Properties
The StatusStrip class is inherited from the ToolStrip ->ScrollableControl->Control classes and hence has all of the common properties supported by a Windows Forms control. Some of these common properties include Font, BackColor, Dock, BackgroundImage, and TextDirection.
We can set the StatusStrip control properties using the Properties Window that looks like Figure 3.
Figure 3
Run-time
The code snippet in Listing 2 sets the Name, Text, BackColor, Font, and ForeColor properties of the the StatusStrip control at run-time.
- StatusStrip dynamicStatusStrip = new System.Windows.Forms.StatusStrip();
- dynamicStatusStrip.Name = "DynamicStatusStrip";
- dynamicStatusStrip.Text = "statusStrip1";
- dynamicStatusStrip.BackColor = Color.OrangeRed;
- dynamicStatusStrip.Font = new Font("Georgia", 14, FontStyle.Italic);
- dynamicStatusStrip.ForeColor = Color.White;
Listing 2
Docking a StatusStrip
Unlike the old StatusBar control, now we can dock a StatusStrip control on a Form. The Dock property of the StatusStrip is used to set docking. A StatusStrip control can be docked at top, bottom, left, right, or center.
Figure 4 shows a top docked StatusStrip control.
Figure 4
Run-time
The Dock property is a type of DockStyle enumeration. The code snippet in Listing 3 sets the Dock property at run-time.
- dynamicStatusStrip.Dock = DockStyle.Top;
Listing 3
Background Image
The BackColor property sets the background color of a StatusStrip control. We can also set an image as the background color of a StatusStrip control. The BackgroundImage property is used to set the background image of a StatusStrip control. When you click on the BackgroundImage property in the Property designer, you will see the Resource Editor that looks like Figure 5, where you can browse an image or use local resources using the Import button.
Figure 5
We can also set the layout of the background image using the BackgroundImageLayout property that can be stretched, tiled, centered, centered or zoomed as you can see in Figure 6.
Figure 6
Run-time
The BackgroundImage property takes an Image type. We can use an ImageList to manage all the images. An ImageList is an object of Image objects. The code snippet in Listing 4 creates an ImageList object and adds an image to it from a file. The code then sets the BackgroundImage and BackgroundImageLayout properties.
- ImageList dynamicImgList = new ImageList();dynamicImgList.Images.Add(Image.FromFile(@"C:\Images\Neel\18.jpg"));
- dynamicStatusStrip.BackgroundImage = dynamicImgList.Images[0];
- dynamicStatusStrip.BackgroundImageLayout = ImageLayout.Center;
Listing 4
Text Direction
The TextDirection is another useful property that was missing in the StatusBar control. Using the TextDirection property, we can set the text direction of the StatusStrip control. It has three properties – Horizontal, Vertical90 and Vertical270. Figure 7 shows Verical90 text of a StatusStrip control.
Figure 7
Run-time
The TextDirection property is a type of ToolStripTextDirection enumeration. The code snippet in Listing 5 sets the Dock property at run-time.
- dynamicStatusStrip.TextDirection = ToolStripTextDirection.Vertical270;
Listing 5
Sizing Grip, Grip Style, and Layout Style
The sizing grip is a little sizing handle in the lower right corner of a control. Using the SizingGrip property is used to enable the sizing handle. The SizingGrip property works with the GripStyle and the LayoutStyle properties. The GripStyle property is used to show or hide the grip style. The LayoutStyle property as shown in Figure 8 represents the layout type of the grip.
Figure 8
Note
The sizing grip will not be displayed unless you also set the LayoutStyle property to one of the overflow values.
Run-time
The code snippet in Listing 6 sets the SizingGrip, the GripStyle, and the LayoutStyle properties at run-time.
- dynamicStatusStrip.SizingGrip = true;
- dynamicStatusStrip.GripStyle = ToolStripGripStyle.Visible;
- dynamicStatusStrip.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
Listing 6
Adding StatusStrip Items
A StatusStrip control is nothing but a container for the child controls. A StatusStrip control can host several child controls. These child controls are represented by the Items property. If you click on the Items property of the StatusStrip control, you will see Items Collection Editor where you can add, update, and delete the child controls. If you click on the Items property, you will see the Item Collection Editor where you can select an item type from the DropDown and click Add button to add the child control. The DropDown has four item types and these items are the ToolStripStatusLabel, ToolStripProgressBar, ToolStripDropDownButton, and ToolStripSplitButton.
Now, let's add one item of each control type as you can see from Figure 9. Once you add an item, you will see all the properties in the right side properties window.
Figure 9
Here comes the good part. All of the child controls placed on a StatusStrip control have their own unique IDs and can be used as independent controls. That means, you can use and control these child controls similar to any other control on a Form.
After adding child controls and setting their properties, the final StatusStrip control looks like Figure 10.
Figure 10
We can also set the properties, events and methods of a child control by right click on the child control on the StatusStrip control and click on the Properties window like any other control. If you right click and select Properties of the ProgressBar child control, you will see something like Figure 11.
Figure 11
StatusBar Enabled Application
Alright! Let's build a StatusBar enabled application.
I create a Windows Forms application and add a StatusStrip control to the Form. I also add three Button controls to the Form. After that, I add two StatusStripStatusLabel, one StatusStripDropDownButton, and one StatusStripProgressBar child controls to the Form.
Now I add three menu items to the StatusStripDropDownButton by using its Items property and change its text and foreground colors to Red, Green, and Blue. I also add the Click event handlers for these menu items. The event handler of menu items looks like following:
- private void RedMenuItemClick(object sender, EventArgs e)
- {
-
- }
The final Form with the StatusStrip control and its items looks like Figure 12.
Figure 12
On the Start button click event handler, I set the Value and the Text properties of the ProgressBar as listed in Listing 7.
- private void StartButton_Click(object sender, EventArgs e)
- {
- toolStripProgressBar1.Value = 90;
- ProgressLabel.Text = toolStripProgressBar1.Value.ToString();
- }
Listing 7
One last thing I need to do is, set the background color of the StatusStrip control to Red, Green, and Blue on the Red, Green, and Blue menu item click event handlers. See Listing 8.
- private void RedMenuItemClick(object sender, EventArgs e)
- {
- statusStrip1.BackColor = Color.Red;
- }
-
- private void GreenMenuItemClick(object sender, EventArgs e)
- {
- statusStrip1.BackColor = Color.Green;
- }
-
- private void BlueMenuItemClick(object sender, EventArgs e)
- {
-
- statusStrip1.BackColor = Color.Blue;
- }
Listing 8
That's it.
No let's build and run the application.
Click on the Menu items to change the background color of the StatusBar and click the Start button to see the current value of the ProgressBar control.
Summary
The StatusStrip control represents a StatusBar in Windows Forms 4. The StatusStrip control is much more powerful than the StatusBar control in the previous versions of Windows Forms. The StatusStrip control can not only display the text status in the status bar area but also allows us to host other child controls such as a DropDownMenu, ProgressBar, and a SplitButton. In this tutorial, I demonstrated how to build a StatusBar enabled application using the StatusStrip control.
In my next article, I plan to cover details of the StatusStrip child controls.
Further Readings
Here are some articles on the StatusBar and the StatusStrip controls.