ToolStrip Basics
The ToolStrip is the basis of the StatusStrip, MenuStrip, and ContextMenuStrip controls you'll consider in this chapter, and a slick stand-alone control of its own. To get off to a quick start with ToolStrip, drag it onto a form, select it, and choose Insert Standard Items from the smart tag. This adds a basic set of buttons including File, Open, Save, and Print, the editing commands Cut, Copy, and Paste, and a Help button.
When you run this example, you'll immediately notice that the ToolStrip sports a slick new interface that's far better than the miserably out-of-date ToolBar. Some of its niceties include:
• It's flat, so there aren't any visible button edges (although you can add etched separator lines).
• It supports hot tracking, so as you hover over a button, it's highlighted.
• It supports Windows XP themes, so the gradient background that's painted as the bar background automatically uses the active color scheme.
• It provides automatic overflow menus. That means when the windows resized so that not all the buttons can fit, an arrow icon is added at the end of the menu. If you click that arrow, you'll see a drop-down menu with the missing items (see Figure 14-1). You can disable the overflow behavior by setting CanOverflow to false.
Overall, the ToolStrip closely matches the polished toolbars introduced in Microsoft Office XP (and present in later versions).
- ¿^^du^jt V -1
Figure 14-1. Automatic overflow menus in the ToolStrip
Figure 14-1 shows a standard horizontal ToolStrip, which is what you get when you first drop a ToolStrip onto a form. However, you can change the direction using the ToolStrip. LayoutStyle property, using any of the options described in Table 14-1.
Table 14-1. LayoutStyle Values (from the ToolStripLayoutStyle Enumeration) Value Description
HorizontalStackWithOverflow Arranges items in a horizontal line from left to right, adding any leftovers to the overflow menu (on the right).
VerticalStackWithOverflow Arranges items in a single column of buttons, and adds an overflow menu to the bottom if needed.
StackWithOverflow Uses either HorizontalStackWithOverflow or
VerticalStackWithOverflow, depending on whether the ToolStrip is docked to the top or side of its container. This is the default.
|
Value |
Description |
|
Flow |
Tiles items from left to right and then downward to fill the |
|
available space. No overflow menu is created—instead, the | |
|
ToolStrip is enlarged to fit all the items. | |
|
Table |
Arranges items in a grid, from left to right and then down. To |
|
make this work, you need to cast the ToolStrip.LayoutSettings | |
|
to TableLayoutSettings, and then set the desired number of | |
|
columns. The ToolStrip is then expanded (with new rows) to | |
|
fit all the items. |
Usually, when you use horizontal layout you'll want to dock the ToolStrip to the top or bottom of your form. When you use a vertical layout you'll dock to the left or right sides. In fact, when you set the ToolStrip.Dock property, the ToolStrip automatically sets the LayoutStyle property to match (although you can modify it afterward).
■Tip Although docked horizontal and vertical toolbars are most common, you don't need to dock your ToolStrip anywhere. If you don't, it remains fixed in place on the form wherever you've positioned it, just like any other control. Later in this chapter, you'll learn to use the ToolStripContainer to allow users to rearrange ToolStrip controls.
The ToolStrip is outfitted with a wide range of features, and it's impossible to introduce them all at once. Instead, the following sections will take you through a series of common ToolStrip tasks, from the relatively simple (for example, handling button clicks) to the much more complex (customizing the painting logic).
Post a comment