Basic StatusBar
There are two ways to use a status bar. You can display a single piece of text, or a combination of panels that can contain text or icons. To use a simple status bar, just set the ShowPanels and Text properties. This doesn't provide any border around the status bar (see Figure 6-13), so you might want to add an extra horizontal line above using a group box control.

Figure 6-13: The simplest possible status bar
statusBar.ShowPanels = false;
statusBar.Text = "Ready";
You can also tweak the Font for the current text and the SizingGrip property, which enables or disables the grip lines on the bottom right corner of the status bar (where the user can grab to resize the form).
Alternatively, you can create a status bar that contains different pieces of information. This information is represented as a collection of StatusBarPanel objects in the Panels property. You can configure this collection at design-time using the custom designer, or at runtime in code.
StatusBarPanel pnlNew = new StatusBarPanel();
pnlNew.Text = "Ready";
statusBar.Panels.Add(pnlNew);
The StatusBarPanel object provides several appearance-related properties (see Table 6-11).
Table 6-11: StatusBarPanel Properties
PropertyDescription
AlignmentDetermines how the text is aligned. The default is HorizontalAlignmnent.Left.
AutoSizeDetermines how the panel should be sized using one of the values from the StatusBarPanelAutoSize enumeration. Contents means the panel is automatically sized to fit the text length. None means that the panel is fixed in size (based on its Width property). Spring means that the panel takes up all available space. You can use this for the last panel, or you can set several panels to spring (and grow proportionately as the status bar is expanded).
BorderStyleEach panel can be displayed with the default sunken border, a different border, or no border at all.
IconYou can display an icon in a panel along with its text by setting this property to a valid Icon object. The icon appears at the left of the panel.
MinWidthIf you are using autosizing, this is the minimum width the panel is given.
TextThe text that appears in the panel.
ToolTipTextThe tooltip that appears when the user hovers the mouse over the panel.
WidthThe current width of the panel, in pixels.
The code that follows creates a more sophisticated multipaneled status bar, shown in Figure 6-14:

Figure 6-14: A status bar with panels
StatusBarPanel pnlStatus = new StatusBarPanel();
pnlStatus.Text = "Ready";
pnlStatus.Icon = new Icon(Application.StartupPath + "\\active.ico");
pnlStatus.AutoSize = StatusBarPanelAutoSize.Contents;
StatusBarPanel pnlConnection = new StatusBarPanel();
pnlConnection.Text = "Connected to " + serverName;
pnlConnection.AutoSize = StatusBarPanelAutoSize.Spring;
statusBar.Panels.Add(pnlStatus);
statusBar.Panels.Add(pnlConnection);
The StatusBar raises a PanelClick event that provides information about the mouse position and the StatusBarPanel object that was clicked. You can respond to this event to create system tray-like functionality. For example, you could add an icon that can be clicked to change modes. This technique can be neat, but it is often not obvious to the average end user.
Tip
You will probably want to use form-level variables to store a reference to the StatusBarPanel objects that you need to update regularly.
Synchronizing the StatusBar to a Menu
There is no automatic way to connect menu item Help text to a status bar panel (some C++ programmers may remember this was a feature of the MFC application wizard). Before I tell you how you can do this, I should warn you why you might not want to.
The problem with putting menu Help text in the status bar is that even advanced users rarely associate two controls together when they are separated by so much physical space. In other words, those who need the Help text will have no idea that it is there. Even worse, most users don't understand that they can hover over a menu item to select it, and probably just understand clicking a menu item, which immediately activates without providing the helpful information. Many critics complain that the status bar Help text feature is just a gambit to use up extra space in the status bar (see Figure 6-15).

Figure 6-15: An unhelpful status bar in Microsoft Paint
If you still want to create this interface, all you need to do is handle the MenuItem.Select method. This technique is very similar to the example shown in Chapter 4. However, the MenuItem class doesn't provide any Tag property where you can store additional information (like the Help text). Instead, you have to keep a hashtable collection handy, and use the control reference to look up the Help string.
// This method handles mnuOpen.Click, mnuNew.Click, mnuSave.Click, and so on.
private void mnu_Click(object sender, System.EventArgs e)
{
Status.Text = HelpCollection[sender];
}
You could also perform the same trick using a custom menu control, and overriding the OnSelect() method. It's similar to the technique used in the previous examples for the MenuItem interaction with a ToolBar. Chapter 7 shows a third way to implement this type of design-this time using a custom extender provider.