The Tool StripContainer

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. (See Chapter 2 for a more detailed discussion about z-order.) 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 manage 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.




Top Panel

Left Panel

Content Panel

Right Panel

Bottom Panel

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

+1 0

Post a comment