3.1. Add Tool Strips to Your Application.NET 2.0 introduces a new ToolStrip control that provides a modern Office look for toolbars, handles Windows themes, and provides access through the toolbar to a plethora of controls, including buttons, lists, menus, labels, and more. 3.1.1. How do I do that?To get started, open a new Windows application and call it Toolbars. Make sure the Toolbox is open and drag a ToolStripContainer control onto your form. Click the ToolStripContainer smart tag for a list of ToolStripContainer tasks. The ToolStripContainer control allows you to specify that the panel be visible at the top, bottom, left, or right of the application Window, as shown in Figure 3-1. Choose top only. Figure 3-1. The ToolStripContainer smart tagDrag a ToolStrip control from the Toolbox onto the ToolStripContainer control, and click the smart tag to open the ToolStrip Tasks menu. Click Insert Standard Items to add the standard tool strip items, as shown in Figure 3-2. Figure 3-2. Adding a tool stripClicking the Edit Items option at the bottom of the ToolStrip Tasks menu opens the Items Collection Editor. This allows you to add new items to the tool strip as well as to manipulate the strip itself, setting such properties as CanOverflow, as shown in Figure 3-3. Figure 3-3. Editing the tool strip and adding itemsThe CanOverflow property can be set through the properties window, the smart tag, or dynamically at runtime. If you enable this property and then shrink the window so that the tool strip no longer fits, a special menu appears at the right that makes the missing buttons available, as shown in Figure 3-4. Figure 3-4. The overflow menuThe ToolStrip control is designed to hold a number of ToolStripItem controls, including those listed in Table 3-1.
Figure 3-5. The drop-down buttonFigure 3-6. The split buttonAll the controls housed in a ToolStrip control derive from the ToolStripItem class, and all share the Text and Image properties, making it easy to work with each of them in a uniform way. If you click an item in your tool strip and then click the event button in the properties window (the one with the lightening bolt) to see its associated events, you'll find that the menu item supports a variety of events. As with other, standard controls, double-clicking opens the default event (Click), making it trivial to create an event handler for clicking a ToolStrip control. 3.1.2. What about......other controls? The look and feel of the tool strip interface is consistent with the look and feel of Office 2003. Do controls provide support for Office look and feel? Yes, the StatusStrip, MenuStrip, and ContextMenuStrip controls also provide support for the look and feel of Office 2003. Note that these controls replace the StatusBar, MainMenu, and ContextMenu controls of Windows Forms 1.x, in much the same way that ToolStrip replaces the old ToolBar control. You can drag a MenuStrip control into the same ToolstripContainer as the ToolStrip control, and the form will place them appropriately, with the menu bar directly above the toolbar, as shown in Figure 3-7. Figure 3-7. The MenuStrip added...what if I want to get to the old Toolbar control? It isn't in my Toolbox. To access the old versions of these controls you must manually add them to the Visual Studio Toolbox. Right-click the Toolbox and select Choose Items.... This step opens the Choose Toolbox Items dialog, where you can select the traditional ToolBar control, as shown in Figure 3-8. Figure 3-8. Adding the traditional ToolBar control3.1.3. Where can I learn more?You'll find extensive articles on using the new ToolStrips on the .NET Framework Windows Forms site at http://www.windowsforms.net. |