Tool, Menu, and Status Strips—The ToolStripContainer
| Visual C# Tutorials |
| C# Tutorials |
|
© 2006 Matthew MacDonald |
The ToolStripContainer
So far, the ToolStrip examples you’ve seen have used docked ToolStrip objects. This is a quick way to build simple forms, and it’s ideal if you intend to have only a single ToolStrip visible. However, there’s another option—you can embed your ToolStrip inside a ToolStripContainer.
The ToolStripContainer allows more than one docked ToolStrip control to share space. For example, imagine you create a control with three ToolStrip objects. If you dock them all to the top, they appear in three separate rows, one above the other, depending on the order in which you created them. The first created object is at the top, because it has the lowest z-index. To change this, you can right-click the control you want on top and select Bring To Front.
But what do you do if you want more than one ToolStrip control appear on the same row, side by side but similarly docked to the top edge? You could avoid docking altogether and position them absolutely, but this causes tremendous headaches with ToolStrip resizing. Namely, you’ll need to tweak the ToolStrip size to accommodate newly added buttons, and write code to mange overflow menus and implement the proper sizing behavior when the window is resized. Fortunately, the ToolStripContainer saves you the trouble with an elegant solution.
Essentially, the ToolStripContainer is a group of five panels. There are four ToolStripPanel controls, one for the top, bottom, left, and right edges, and a ContentPanel for the center region, where you can place the rest of the window content. Figure 14-8 shows this design. Usually, you’ll dock the ToolStripContainer to fill the form, so that its edges are the same as the form’s edges.

Figure 14-8. The ToolStripContainer at design time
When the ToolStripContainer is first created, these four panels are hidden. However, as soon as you place a ToolStrip on one of the edges, the closest panel is resized to fit the ToolStrip. The neat part of this design is the fact that the ToolStrip objects don’t use any docking—instead, they’re placed in terms of the panel, and the panel is docked in the right place. By default, the ToolStripContainer panels use a shaded background like the ToolStrip.
Figure 14-9 shows a ToolStripContainer with several identical ToolStrip objects. To make it easier to see the different panels, the background color of the content panel has been changed to white. Now, you can place more than one ToolStrip on the same row or column, and you also can click on the ToolStrip sizing grip at runtime and drag it from one place to another. A user can apply this technique to rearrange a group of adjacent ToolStrip objects, or to drag a ToolStrip from one panel to another (for example, from the top of the window to the right side). The ToolStripContainer provides the necessary dragging cues. For example, as you drag the ToolStrip, a rectangle outline shows you the new position. When you approach one of the sides, the ToolStrip snaps neatly into place with the correct orientation.
Tip You can set the ToolStrip.Stretch property to true to force a ToolStrip to fill the whole row (for a horizontal ToolStrip) or column (for a vertical ToolStrip). This property is intended primarily for displaying menus. It has no effect unless the ToolStrip is inside a ToolStripPanel.

Figure 14-9. Rearranging ToolStrip objects at runtime
If you want to add a ToolStrip into a ToolStripContainer at design time, you first must expand the panel where you want to place it. To do this, use the arrow buttons that appear along each edge of the ToolStripContainer. These buttons are a design-time convenience, and they don’t appear at runtime. For example, if you want to add a ToolStrip to the right edge, start by clicking the right arrow button to expand the panel. You can then drop the ToolStrip into the exposed panel area. When you first add the ToolStripContainer, the top panel begins with its surface exposed.
Tip You can add a ToolStripContainer directly from the Visual Studio toolbox. However, if you have an existing ToolStrip that you want to place into a ToolStripContainer, just select it and choose Embed in ToolStripContainer from the smart tag. Visual Studio will create a new ToolStripContainer and place your existing ToolStrip inside.
|

