Section 3.1. Add Tool Strips to Your Application


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 tag


Drag 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 strip


Clicking 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 items


The 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 menu


The ToolStrip control is designed to hold a number of ToolStripItem controls, including those listed in Table 3-1.

Table 3-1. ToolStripItem controls

ToolStripItem control

Description

ToolStripButton

An item the user can click. Buttons can be text, images, or both. The toolbars shown in Figure 3-4 illustrate standard buttons with images.

ToolStripLabel

An item the user cannot click. Labels can be text, images, or both.

ToolStripSeparator

Divides items with a thin indented line. You can see these in any standard menu. They also are shown in the toolbar in Figure 3-2.

ToolStripDropDownButton

Creates drop-down menus with items. ToolStripDropDownButton shows its items as a menu, with the ability to check items, as shown in Figure 3-5.

ToolStripSplitButton

Creates a two-part control. The left part is a standard button, and the right part is a drop-down menu, as shown in Figure 3-6.

ToolStripComboBox ToolStripTextBox ToolStripProgressBar

These are just like the equivalent Windows controls, except they derive from ToolStripControlHost.


Figure 3-5. The drop-down button


Figure 3-6. The split button


All 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 control


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



Visual C# 2005(c) A Developer's Notebook
Visual C# 2005: A Developers Notebook
ISBN: 059600799X
EAN: 2147483647
Year: 2006
Pages: 95
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net