User:Foxall/09

Ka Wiktionary

Hour 9. Adding Menus and Toolbars to Forms[wax ka badal]

The use of a Graphical User Interface (GUI) for interacting with and navigating programs is one of the greatest features of Windows. In spite of this, a fair number of Windows users still rely primarily on the keyboard, preferring to use a mouse only when absolutely necessary. Data-entry people in particular never take their hands off the keyboard. Many software companies receive support calls from angry customers because a commonly used function is accessible only by using a mouse. Menus are the easiest way for a user who relies on the keyboard to navigate your program. Visual Studio and C# make it easier than ever to create menus for your applications. In this hour, you'll learn how to build, manipulate, and program menus on a form. In addition, I'll teach you how to use the Toolbar control to create attractive and functional toolbars. Finally, you'll learn how to "finish off" a form with a status bar.

The highlights of this hour include the following:

  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec1.htm#ch09lev2sec1 Adding, moving, and deleting menu items]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec1.htm#ch09lev2sec3 Creating checked menu items]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec2.htm#ch09lev1sec2 Programming menus]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec2.htm#ch09lev2sec4 Implementing context menus]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec2.htm#ch09lev2sec5 Assigning shortcut keys]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec3.htm#ch09lev1sec3 Creating toolbar items]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec3.htm#ch09lev2sec8 Defining toggle buttons and separators]
  • [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch09lev1sec4.htm#ch09lev1sec4 Creating a status bar]

>

Building Menus

When I said that C# makes building menus easier than ever, I wasn't kidding. Building menus is now an immediately gratifying process. I can't stress enough how important it is to have good menus, and now that it's so easy to do, there is no excuse for not putting menus in an application.

graphics/bulb.gif When running an application for the first time, users often scan the menus before opening a manual. (Most users never open the manual!) When you provide comprehensive menus, you make your program easier to learn and use.
Adding Menu Items 

Adding menus to a form is accomplished by way of a control: the Main Menu control. The Main Menu control is a bit odd in that it's the only control I know of that sits at the bottom of the form in the space reserved for controls without an interface (like a Timer control), yet actually has a visible interface on the form. Start by creating a new Windows Application project named Menus and More.

  1. Change the name of the default form to fclsMenusAndMore, set its Text to Menus and More, and change the Main entry point of the project to reference fclsMenusAndMore instead of Form1.
  2. Next, add a new Main Menu control to your form by double-clicking the MainMenu item in the toolbox. As you can see, the control is added to the pane at the bottom of the form designer. Take a look at the top of the form�you'll see the text Type Here (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig01 Figure 9.1]).
Figure 9.1. A menu has no items when first added to a form.

graphics/09fig01.jpg

  1. Click this text and type &File. As you begin typing, C# displays two new boxes that say Type Here (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig02 Figure 9.2]).
Figure 9.2. Creating a menu item automatically prepares the control for more items.

graphics/09fig02.jpg

Notice the Properties window (if it's not visible, press F4 to show it). The text you just entered created a new menu item. Each menu item is an object; therefore, the item has properties. (You may have to press Tab to commit your entry and then click the text you typed once more to see its properties.)MenuItem1 isn't very descriptive, so change the name of the item to mnuFileMenu.

graphics/newterm.gif You may have been wondering why I had you enter the ampersand (&) in front of the word File. Take a look at your menu now, and you'll see that C# doesn't display the ampersand; instead, it displays the text with the F underlined, as in File. The ampersand, when used in the Text property of a menu item, tells C# to underline the character immediately following it. For top-level menu items, such as the File item you just created, this underlined character is known as an accelerator key. Pressing Alt+ an accelerator key opens the menu as if the user had clicked it. You should avoid assigning the same accelerator key to more than one top-level menu item on a given menu. When the menu item appears on a drop-down menu, in contrast to being a top-level item, the underlined character is called a hotkey. When a menu is visible (open), the user can press a hotkey to trigger the corresponding menu item the same as if it was clicked. Again, don't use the same hotkey for more than one item on the same menu.
  1. Click the Type Here text that appears to the immediate right of the File item and enter the text &Help. C# gives you two more Type Here items, the same as when you entered the File item. Adding new menu items is a matter of clicking a Type Here box and entering the text for an item.
  2. Press Tab to commit your entry and then click the text you typed once more to select it. Change the name of your new menu item in the Properties window to mnuHelpMenu.
graphics/bookpencil.gif If you click a Type Here box below an existing menu item, you'll add a new item to the same menu as the item above the box. If you click the Type Here box to the right of a menu item, you'll create a submenu using the menu to the left of the box as the entry point for the submenu. As you've already seen, clicking the Type Here box along the top of the menu bar creates a top-level menu.
  1. Click once more on the File item to display a Type Here box below the item. Click this box and enter the text &Quit.
  2. Press Tab to commit your entry and then click the new item once more to select it. Change the name of the new item to mnuQuit. Now is a good time to save your work, so click Save All on the toolbar.
Moving and Deleting Menu Items 

Deleting and moving menu items are even easier processes than adding new items. To delete a menu item, right-click it and choose Delete from the context menu that appears. To move an item, drag it from its current location and drop it in the location in which you want it placed.

 Creating Checked Menu Items 

A menu item that isn't used to open a submenu can display a check mark next to its text. Check marks are used to create menu items that have state�the item is either selected or it is not selected. You're now going to create a checked menu item. Click the Type Here box below the Quit menu item and enter Ask before closing and then change the name of this new item (remember to press Tab to commit your entry and then click the item again to select it) to mnuAskBeforeClosing. Next, change the Checked property of the new item to true. Notice that the menu item now has a check mark next to it (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig03 Figure 9.3]).

Figure 9.3. Menu items can be used to indicate state.

graphics/09fig03.jpg

Press F5 to run the project. The menu will appear on your form, just as you designed it (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig04 Figure 9.4]). Click the File menu to open it and then click Quit; nothing happens. In the next section, I'll show you how to add code to menu items to make them actually do something. Stop the project before continuing.

Programming Menus

As I've said before, every menu item is a unique object�this is why you were able to change the name for each item you created. Although individual menu items aren't controls, per se, adding code behind them is very similar to adding code behind a control. You're now going to add code to the Quit and Ask Before Closing menu items.

  1. Click the File menu now to open it.
  2. Double-click the Quit menu item. Just as when you double-click a control, C# displays the code editor with the default event for the menu item you've clicked. For menu items, this is the Click event.
  3. Enter the following code:
this.Close();

As you know by now, this code closes the current form, which has the effect of stopping the project because this is the only form and it's designated as the Main entry point object.

  1. Switch back to the form designer (click the Form1.cs [Design] tab).

You're now going to create the code for the Ask Before Closing button. This code will invert the Checked property of the menu item; if Checked = True, it will be set to false and vice versa.

  1. Double-click the Ask Before Closing item to access its Click event and enter the following code:
mnuAskBeforeClosing.Checked = (!mnuAskBeforeClosing.Checked);

The logical negation operator (!) is used to perform a negation of a Boolean value. Don't worry, I discuss this in detail in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch13.htm#ch13 Hour 13], "Performing Arithmetic, String Manipulation, and Date/Time Adjustments." For now, realize that if the current value of the Checked property is true, (!Checked) returns false. If Checked is currently false, (!Checked) returns true. Therefore, the checked value will toggle between true and false each time the menu item is clicked.

  1. Press F5 to run the project. Open the File menu by pressing Alt+F (remember, the F is the accelerator key).
  2. Next, click the Ask Before Closing button�it becomes unchecked. Click the menu item once more and it becomes checked again.
  3. Click it a third time to remove the check mark, and then click Quit to close the form.

Did you notice that you weren't asked whether you really wanted to quit? This is because the quit code hasn't been written to consider the checked state of the Ask Before Closing button.

  1. Return to the Click event of the Quit button and change its code to look like this:
if (mnuAskBeforeClosing.Checked)
{
 if (MessageBox.Show("Do you really wish to exit?","Quit
 Verification",MessageBoxButtons.YesNo) == DialogResult.No)
 return;
}
this.Close();

Now when the user selects the Quit button, C# considers the checked state of the Ask Before Closing menu item. If the item is checked, C# asks users whether they really want to exit. If a user chooses No, the procedure quits and the form doesn't unload. This code may be a bit foreign to you now, but you'll learn the ins and outs of making decisions (executing code based on conditions) and message boxes in later hours.

  1. Press F5 to run the project. Open the File menu, click the Ask Before Closing item to select it, and then click Quit.
  2. This time, C# asks you to confirm your intentions rather than immediately closing the form. Go ahead and close the form, and then click Save All on the toolbar to save your work.
graphics/bulb.gif When designing your menus, look at some of the many popular Windows applications available and consider the similarities and differences between their menus and yours. Although your application may be quite unique and therefore may have very different menus from other applications, similarities probably exist as well. When possible, make menu items in your application follow the same structure and design as similar items in the popular programs. This will shorten the learning curve of your application, reduce user frustration, and save you time.
Implementing Context Menus 

Context menus are the pop-up menus that appear when you right-click an object on a form. Context menus get their name from the fact that they display context-sensitive choices�menu items that relate directly to the object that's right-clicked. Most C# controls have a default context menu, but you can assign custom context menus if you desire. Add a new text box to the form and set its properties as follows:

Property Value
Name txtMyTextbox
Location 96,122
Size 100,20
Text (make blank)

Press F5 to run the project, and then right-click the text box to display its context menu (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig05 Figure 9.5]). This menu is the default context menu for the Text Box control; it is functional but limited. Stop the project now and return to Design view.

Figure 9.5. Most items have a default context menu.

graphics/09fig05.jpg

Creating context menus is very much like creating regular menus. Context menus, however, are created using a different control: the Context Menu control.

  1. Add a new context menu to the form by double-clicking the ContextMenu item in the toolbox. Like the Main Menu control, the Context Menu control is placed in the pane below the form designer. When the control is selected, a Context Menu item appears at the top of the form.
  2. Clicking the Context Menu box opens the context menu, which is empty by default. Click the Type Here box and enter the text Clear text box (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig06 Figure 9.6]). You've just created a context menu with a single menu item.
Figure 9.6. Context menus are edited much like regular menus.

graphics/09fig06.jpg

  1. Change the name of the new menu item to mnuClearTextbox, and then double-click the item to access its Click event.

Enter the following code:

txtMyTextbox.Text = "";
  1. Linking a control to a context menu is accomplished by setting a property. Display the form designer once more, and then click the Text Box control to select it and display its properties in the Properties window.
  2. Change the ContextMenu property of the text box to ContextMenu1; the context menu is now linked to the text box. Press F5 to run the project.
  3. Enter some text into the text box and then right-click the text box; your custom context menu appears in place of the default context menu.
  4. Choose Clear Text Box from the context menu, and the contents of the text box will clear. Stop the project and save your work.
Assigning Shortcut Keys 

If you've spent any time learning a Microsoft application, you've most likely learned some keyboard shortcuts. For instance, pressing Alt+P in any application that prints has the same effect as opening the File menu and choosing Print. You can add the same type of shortcuts to your menus by following these steps:

  1. Click the Main Menu control at the bottom of the form designer, click File on its menu, and then click Quit to select the Quit menu item.
  2. Next, click the Shortcut property in the Properties window and then click the down arrow that appears. This list contains all the shortcut keys that can be assigned to a menu item.
  3. Locate and select CtrlQ (for Quit) in the list (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig07 Figure 9.7]).
Figure 9.7. A shortcut key is assigned using the shortcut property of a menu item.

graphics/09fig07.jpg

  1. Press F5 to run the project once more. Next, press Ctrl+Q, and the application will behave just as though you opened the File menu and clicked the Quit item.
graphics/bulb.gif Although it's not always possible, try to assign logical shortcut key combinations. The meaning of F6 is hardly intuitive, for example, but when assigning modifiers such as Ctrl with another character, you have some flexibility. For instance, the key combination of Ctrl+Q might be a more intuitive shortcut key for Quit than Ctrl+T.

Using the Toolbar Control

Generally speaking, when a program has a menu (as most programs should), it should also have a toolbar. Toolbars are one of the easiest ways for a user to access program functions. Unlike menu items, toolbar items are always visible and therefore are immediately available. In addition, toolbar items have ToolTips, which allow a user to discover a toolbar 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 is the same, regardless of the buttons you choose to use. Toolbars are created using the Toolbar control.

  1. Add a new Toolbar control to your form now by double-clicking the ToolBar item in the toolbox. A new toolbar is then added to the top of your form (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig08 Figure 9.8]). Change the name of the toolbar to tbrMainToolbar.

Every toolbar you've used displays pictures on buttons. The Toolbar control gets the pictures for its buttons from an Image List control.

  1. Go ahead and add an Image List control to the form now and change its name to imgMyPictures.
  2. Add a new 16x16 pixel bitmap to the Images collection of the Image List control (you can use a picture you created or use one from the samples I've made available on the Web site).
  3. When you're finished adding the new button, close the Image Collection Editor and select the Toolbar control on the form.
  4. Set the ImageList property of the toolbar to use the image list you've just created.
Adding Toolbar Buttons Using the Buttons Collection 

Like many other controls you've already learned about, the Toolbar control supports a special collection: the Buttons collection. The Buttons collection contains the buttons that appear on the toolbar. Click the Buttons property in the Properties window and then click the small button that appears; the ToolBarButton Collection Editor displays. The list of button members is empty because new toolbars have no buttons. Click Add to create a new button, and set its properties as follows:

Property Value
Name tbbQuit
ImageIndex 0
Text Quit
ToolTipText Quit this application

Click OK to close the ToolBarButton Collection Editor. Your new button is now visible on the toolbar. As you can see, text appears below the picture in the button, but this is not how most toolbars appear. Not a problem�access the Buttons collection once more and clear the Text property of the button.

 Programming Toolbars 

Unlike menus, where each menu item receives its own Click event, the Toolbar control has one common Click event that fires when the user clicks any button on the toolbar. Double-click the Toolbar control on the form to access the toolbar's ButtonClick event (close the button editor first if it's open). Enter the following code:

if (e.Button == tbbQuit)
 this.Close();
graphics/bookpencil.gif In a nontrivial program, the ideal way to set this up would be to create a method that is called both by the Click event of the menu item and by clicking the equivalent toolbar button. This reduces duplication of code and eliminates bugs (the Quit button doesn't honor the Ask Before Closing option in this example, even though the menu item does).

The e object is discussed in detail in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch11.htm#ch11 Hour 11], "Creating and Calling Methods." For now, realize that the Button property of the e object is an object property that holds a reference to the button that is clicked. This code uses an if statement to determine if the clicked button is the Quit button. If the Quit button was clicked, the form closes. When you have many buttons on a toolbar, a switch statement is much more useful than an if statement, as you'll see in the next section. (Decision-making constructs such as if and switch statements are discussed in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch14.htm#ch14 Hour 14], "Making Decisions in C# Code.")

 Creating Toggle Buttons 

The button that you've created for your toolbar is a standard push-style button. When the user clicks it with the mouse, the button will appear to be pressed while the user holds down the mouse button and will return to a normal state when the user releases the mouse button. Although this is the style you'll use for most of your toolbar buttons, the Toolbar control supports other styles as well. One such style is the toggle button. A toggle button, much like the check mark of a menu item, is used to denote state. When a toggle button is clicked, it appears in a pressed state and stays that way until clicked again, in which case it returns to its normal appearance. Microsoft Word has a number of such buttons. For instance, the paragraph alignment buttons are all toggle buttons�the button that corresponds to the current paragraph's alignment appears to be pressed.

Add a new button to your toolbar now and change its name to tbbInvisible. Change its Text property to Invisible and its Style to ToggleButton. Click OK to close the editor and the new button will appear on the toolbar. Take note that you didn't have to designate a picture for the toolbar item (but you usually should). Because you don't want the toolbar's height to be larger than necessary, change the TextAlign property of the Toolbar control to Right. Your toolbar should now look like the one in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig09 Figure 9.9].

Again, double-click the toolbar to access its ButtonClick event. Now that there are two buttons, the simple if statement is no longer suited to determining the button pushed. Change the code in your procedure to match the following:

switch (tbrMainToolbar.Buttons.IndexOf(e.Button))
{
 case 0:
 this.Close();
 break;
 case 1:
 txtMyTextbox.Visible = (!tbbInvisible.Pushed);
 break;
}

This code is a bit more complex than the previous code. The switch construct compares one value to many possible values, looking for a match. In this case, the IndexOf() method of the Buttons collection is used to determine the index of the clicked tool button (the first button is 0, the second is 1, and so forth). The index is then compared to values using the case statements, and when a match is found, the appropriate code executes. Again, don't worry too much about the details of the switch statement, because you'll learn everything you need to know about it in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/ch14.htm#ch14 Hour 14], "Making Decisions in C# Code." The new statement that you're interested in at the moment is where the Visible property of the text box is set. Remember, the logical negation operator (!) negates a value. Therefore, this statement sets the Visible property of the text box to the negation of the pushed state of the tbbInvisible button. In other words, when the button is pushed, the text box is hidden, and when the button is not pushed, the text box is visible.

Press F5 to run the project now and notice that the text box is visible. Click the Invisible button on the toolbar and note that its appearance changes to a pushed state and the text box becomes hidden (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig10 Figure 9.10]). Click the button again to return the button's state to normal, and the text box reappears. When you're finished, stop the project and save your work.

Figure 9.10. Toggle-style buttons appear "pressed" when clicked.

graphics/09fig10.jpg

 Creating Separators 

As you add more and more buttons to a toolbar, it becomes evident that you need a way to logically group buttons. Placing related buttons next to one another is a great start toward building a good toolbar. However, a toolbar may be a bit difficult to use even if its buttons are placed in a logical order, unless the buttons are separated into groups. Placing a space between sets of related buttons creates button groups. You're now going to add a separator space to your toolbar.

Add a new button to your toolbar using the Buttons collection in the Properties window. Change its name to tbbSeparator1 and change its Style to Separator. When you change the Style property of the button to Separator, it disappears from the toolbar, or at least it seems to. When a button is designated as a separator, it's simply an empty placeholder used to create a space between two buttons. Because this separator is at the end row of buttons, you can't see it. Move it to the second position by selecting it in the ToolBarButton Collection Editor and clicking the up arrow that appears to the right of the Members list; this arrow and the one below it are used to move a button up or down in the list. Click OK to save your changes and your toolbar will now have a space between the two buttons (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig11 Figure 9.11]).

Figure 9.11. Separators are used to create spaces between groups of buttons.

graphics/09fig11.jpg

Press F5 to run the project once more and click the Invisible button; the text box no longer becomes hidden. This is a common problem when working with toolbars. Remember how the switch statement looks at the index of the clicked button? The index is the ordinal position of a button in the Buttons collection. When you changed the order of the buttons by moving the separator button up in the list, you changed the index of the separator button and the button it displaced�the tbbInvisible button. The tbbInvisible button now has an index of two (because it's the third button). Change the case statement in the ButtonClick event that compares the case to 1 so that it compares it to 2, and your code will work again. As you fine-tune your toolbars, you need to be conscious of any code that's been written to work with the indexes of buttons in the Buttons collection.

 Creating Drop-Down Menus for Toolbar Buttons 

You need to be familiar with one last type of toolbar button. Create a new button using the Buttons collection in the Properties window. Change the name of the new button to tbbDropdown, clear the Text property, and set the button's Style property to DropDownButton. Finally, set the DropDownMenu property to ContextMenu1 and click OK to commit your changes. Notice how the button has a drop-down arrow on it. Press F5 to run the project and click the drop-down arrow; the menu that you designated in the DropDownMenu property appears (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig12 Figure 9.12]). As you can see, the DropDownMenu property makes it easy to integrate drop-down menus with your toolbars.

>

Creating a Status Bar

The last control I'm going to show you is the Status Bar control. The status bar isn't nearly as fancy, or even as useful, as other controls, such as the Toolbar or the Main Menu. It's also not that hard to work with. Nevertheless, a status bar adds value to an application in that it makes additional information available, and users have come to expect it. In its simplest form, a status bar displays a text caption and sizing grip�the three diagonal lines to the right of the control that the user can drag with the mouse to change the size of the form.

Add a new status bar to the form now by double-clicking the StatusBar item in the toolbox. Change the name of the status bar to sbrMyStatusBar. The Text property determines the text displayed in the left side of the status bar. Notice that the Text is set to the default name of the control. Change the Text property to Menus and Toolbars Example now, and notice how the text in the status bar changes (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig13 Figure 9.13]).

Figure 9.13. Status bars dress out a form.

graphics/09fig13.jpg

If a form's border is sizable, the user can click and drag the sizing grip at the right side of the status bar to change the size of the form. The status bar isn't smart enough to realize when a form's border can't be resized; you'll have to change the SizingGrip property of the status bar to false to hide the grip.

The default behavior of the status bar is quite simple, consisting of text and a sizing grip. However, you can create more complex status bars with this control. The Status Bar control contains a Panels collection. To see how a panel works, select the Panels property in the Properties window and click the small button that appears. On the StatusBarPanel Collection Editor, click Add to create a new panel. Set the Text of the panel to Panel Text, set the AutoSize property to Contents, and click OK to save your changes. Nothing looks different, right? This is because one last thing is required to display the status bar panels. Change the ShowPanels property of the status bar to true now, and the status bar will display its panel (see [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#ch09fig14 Figure 9.14]). You can add multiple panels to a Status Bar control and even tailor the appearance of each panel by changing the border style or displaying an image from a linked Image List control. The status bar is such a simple control that you may overlook using it. However, I encourage you to use it when appropriate.

Summary

Menus, toolbars, and status bars add tremendous value to an application by greatly enhancing its usability. In this hour, you learned how to use the Main Menu control to build comprehensive menus for your applications. You learned how to add, move, and delete menu items and how to define accelerator and shortcut keys to facilitate better navigation via the keyboard. You also saw how toolbars provide shortcuts for accessing common menu items. You learned how to use the Toolbar control to create functional toolbars complete with bitmaps, drop-downs, and logical groupings. Finally, you discovered how to use the status bar to "dress out" the application. Implementing these items is an important part of the interface design process for an application, and you now have the skills necessary to start putting them into your own programs.

>

Q&A

[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#qad1e31135 Q1:] I have a number of forms with nearly identical menus. Do I really need to take the time to create menus for all these forms?
A1: Not as much as you think. Create a Main Menu control that has the common items on it, and then copy and paste the control to other forms. You can then build on this menu structure, saving you a lot of time.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/09.htm#qad1e31145 Q2:] I've seen applications that allow the end user to customize the menus and toolbars. Can I do that with the C# menus and toolbars?
A2: No. To accomplish this behavior, you would have to purchase a third-party component.
>

Workshop

The Workshop is designed to help you anticipate possible questions, review what you've learned, and get you thinking about how to put your knowledge into practice. The answers to the quiz are in [file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01.htm#app01 Appendix A] Answers to Quizzes/Exercises."

 Quiz
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans01 1:] True or False: Form menu bars are created using the Context Menu control.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans02 2:] To create an accelerator or hotkey, preface the character with a(n):
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans03 3:] If you've designed a menu using a Main Menu control, but that menu isn't visible on the form designer, how do you make it appear?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans04 4:] To place a check mark next to a menu item, you set what property of the item?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans05 5:] How do you add code to a menu item?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans06 6:] Toolbar items are part of what collection?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans07 7:] To create a separator on a toolbar, you create a new button and set what property?
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans08 8:] True or False: Every button on a toolbar has its own Click event.
[file:///C:/Documents%20and%20Settings/dani/Asztal/c%23in24h/app01lev1sec9.htm#ch09ans09 9:] What must you do to have panels appear on a status bar?
Exercises
  1. Modify the code you created for the toolbar that closes the form to take into consideration the checked status of the Ask Before Closing menu item.
  2. Implement a toggle button that works just like the Ask Before Closing menu item.