Generally, when a program has a menu (as most programs should), it should also have a toolbar. Toolbars (called toolstrips in Visual C# 2005 for some reason) are one of the easiest ways for a user to access program functions. Unlike menu items, tool-bar items are always visible and therefore immediately available. In addition, tool-bar items have ToolTips, which enable a user to discover a tool button's purpose simply by hovering the pointer over the button. Toolbar items are really shortcuts for menu items; every item on a toolbar should have a corresponding menu item. Remember that some users prefer to use the keyboard, in which case they need to have keyboard access to functions via menus. The actual items you place on a toolbar depend on the features supported by the application. However, the mechanics of creating toolbars and toolbar items are the same regardless of the buttons you choose to use. Toolbars are created using the ToolStrip control. Follow these steps to add a toolbar to the main form in your Picture Viewer project:
Adding Toolbar Buttons Using the Buttons CollectionLike many other controls you've already learned about, the ToolStrip control supports a special collection: the Items collection. The Items collection contains the buttons that appear on the toolbar. Click the Items property in the Properties window, and then click the small Build button that appears; the Items Collection Editor displays. The list of members shows the toolbar itself, but no buttons because new tool-bars have no buttons. By the Way You are going to be adding three images to your toolbar: One for Open, one for Draw Border, and one for Options. You can download these images from my web-site at www.jamesfoxall.com/books.htm. Take a moment to open the drop-down list in the upper-left corner (see Figure 9.12). This list contains the types of items that can be added to a toolbar. Figure 9.12. Toolbars may contain a number of different types of items.For this example, you will be creating buttons and separators. Feel free to experiment with the different item types in another project. Follow these steps:
You've now created the buttons for your toolbar. There's one last thing you should do, however. Professional designers always separate related groups of tool buttons using a separator. A separator is a vertical line that appears between two buttons. All three of the buttons you've created are relatively unrelated, so you're now going to create separators to isolate them from one another. Follow these steps:
By the Way You can add buttons to the ToolStrip control dynamically like you added menu items by using the default button that appears on the ToolStrip. I chose to have you use the Items Collection Editor instead, though, so that you could see there are often multiple ways to attack a problem. Programming ToolbarsProgramming toolbars is pretty much the same as programming menus. As you will see, Microsoft has chosen to standardize things whenever possible. For example, in the previous edition of .NET you worked with a Toolbar control that had a Buttons collection. In 2005, the Toolbar control is replaced with a ToolStrip control that has an Items collection. The List View control has an Items collection, as does the Tree View control. Seeing a pattern? After you learn how to work with the Items collection of one control, it's an easy transition to work with the Items collection of another. Follow these steps to make your toolbar functional:
Go ahead and save your work and then press F5 to run the project. Clicking the toolbar buttons should now perform the same actions as clicking the menu items. In Hour 10, I'll show you how the two controls can share code. Creating Drop-Down Menus for Toolbar ButtonsAlthough you're not going to use one in this project, be aware that you can create drop-down menus on toolbars. Visual Studio 2005 uses these in a number of places (see Figure 9.14). To create a menu like this, instead of adding a regular button to the toolbar you add a DropDownButton. Doing so creates a submenu just like when you defined regular menus earlier in this hour. Figure 9.14. You can create drop-down menus like these.
|