Programming Menus

   

Programming Menus

As I've said before, every menu item is a unique objectthis 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.

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

  2. Press F5 to run the project. Open the File menu by pressing Alt+F (remember, the F is the accelerator key).

  3. Next, click the Ask Before Closing buttonit becomes unchecked. Click the menu item once more and it becomes checked again.

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

  2. Press F5 to run the project. Open the File menu, click the Ask Before Closing item to select it, and then click Quit.

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


  3. 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 = ""; 
  4. 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.

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

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

  7. 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 Figure 9.7).

    Figure 9.7. A shortcut key is assigned using the shortcut property of a menu item.

    graphics/09fig07.jpg


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


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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