Sams Teach Yourself C# in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Audience and Organization
- Conventions Used in This Book
- Onward and Upward!
- Part I. The Visual Studio Environment
- Hour 1. A C# Programming Tour
- Hour 2. Navigating C#
- Hour 3. Understanding Objects and Collections
- Hour 4. Understanding Events
- Part II. Building a User Interface
- Hour 5. Building FormsPart I
- Hour 6. Building FormsPart II
- Hour 7. Working with the Traditional Controls
- Hour 8. Advanced Controls
- Hour 9. Adding Menus and Toolbars to Forms
- Hour 10. Drawing and Printing
- Part III. Making Things HappenProgramming!
- Hour 11. Creating and Calling Methods
- Hour 12. Using Constants, Data Types, Variables, and Arrays
- Hour 13. Performing Arithmetic, String Manipulation, and Date/Time Adjustments
- Hour 14. Making Decisions in C# Code
- Hour 15. Looping for Efficiency
- Hour 16. Debugging Your Code
- Hour 17. Designing Objects Using Classes
- Hour 18. Interacting with Users
- Part IV. Working with Data
- Hour 19. Performing File Operations
- Hour 20. Controlling Other Applications Using Automation
- Hour 21. Working with a Database
- Part V. Deploying Solutions and Beyond
- Hour 22. Deploying a Solution
- Hour 23. Introduction to Web Development
- Hour 24. The 10,000-Foot View
- Appendix A. Answers to Quizzes/Exercises
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.
- Click the File menu now to open it.
- 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.
-
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. - 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.
-
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. - Press F5 to run the project. Open the File menu by pressing Alt+F (remember, the F is the accelerator key).
- Next, click the Ask Before Closing button—it becomes unchecked. Click the menu item once more and it becomes checked again.
- 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.
-
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. - Press F5 to run the project. Open the File menu, click the Ask Before Closing item to select it, and then click Quit.
- 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.
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 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.
Creating context menus is very much like creating regular menus. Context menus, however, are created using a different control: the Context Menu control.
- 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.
-
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.
-
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 = "";
- 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.
- 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.
- 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.
- 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:
- 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.
- 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.
-
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.
- 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.
Using the Toolbar Control | Next Section

Account Sign In
View your cart