Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved."— Presentation transcript:

1 Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

2 5- 2 Objectives Create menus and submenus for program control Display and use the Windows common dialog boxes Write reusable code in methods and call the methods from other locations

3 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 3 Menus Consist of a menu bar containing menus with list of menu items Use menu items in place of or in addition to buttons to activate a method Menu items are controls with properties and events Create menus with the Visual Studio Menu Designer

4 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 4 Defining Menus Add a MainMenu control to a form from the toolbox The MainMenu control appears in the component tray Enter text at words Type Here

5 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 5 The Text and Name Properties The Text property holds the words that appear on the screen Use the ampersand (&) to specify the key for keyboard access in the Text property The Name property gives the MenuItem a name To assign a Name, append suffix “Menu” for top- level menu names and “MenuItem” for items on the menu

6 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 6 Submenus A new list that pops up under an item on a menu is called a submenu A filled triangle to the right of a menu item indicates a submenu for that menu item Create a submenu by moving to the right of a menu item and typing the next item’s text

7 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 7 Separator Bars A separator bar in a menu groups menu items according to their purpose Two ways to create a separator bar –Type a single hyphen (-) for the text –Right-click on Menu Designer at separator bar position and choose Insert Separator Keep the default Name property of the separator bar control

8 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 8 Creating a Menu – Step-by-Step You are going to create a project with one form and a menu bar that contains these menu items: FileHelp Exit About 1.Create the Menu Items 2.Change the Properties of the Menu Items 3.Write the Code Double-click on a menu item to open the Editor window in the control’s Click event-handling method

9 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 9 Modifying Menu Items Use the Menu Designer to modify menu items –Right-click on the menu bar for options including Delete, Insert New, Insert Separator, and Edit Names –Drag and drop menu items to rearrange

10 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 10 The Enabled Property The Enabled property is true by default An enabled menu item has black text and is available for selection A disabled menu item is grayed out and not available Set the Enabled property at design time or in code at run time

11 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 11 The Checked Property A menu item may contain a check mark next to it to indicate the option is currently selected By default, the Checked property is set to false Change the Checked property at design time or in code at run time

12 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 12 Setting Keyboard Shortcuts Create keyboard shortcuts for your menu items –Select the menu item in the designer –Select the Shortcut property in the Properties window –Select choice from drop down list ShowShortcut property is true by default

13 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 13 Standards for Windows Menus Include keyboard access keys and stick with the standard key assignments Place the File menu on the left end of the menu bar with an Exit command at the end If there is a Help menu, it should be at the right end of the menu bar Any menu item that will display a dialog box should have “…” appended to its Text property

14 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 14 Common Dialog Boxes Common dialog controls are used to display dialog boxes that are provided with Windows Visual Studio.NET supports the following common dialog controls - OpenFileDialog- SaveFileDialog - FontDialog- ColorDialog - PrintDialog- PrintPreviewDialog

15 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 15 Displaying a Common Dialog Box Use the ShowDialog method Example: ColorDialog1.ShowDialog(); A modal dialog box stays on top of the application and must be responded to –Use the ShowDialog method A modeless dialog box does not require a response –Use the Show method

16 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 16 Using the Information from the Dialog Box Color Dialog Box –The color selected by user stored in the Color property –Assign this property to another object Font Dialog Box –The font selected by user stored in the Font property –Assign this property to another object Initialize the Font or Color property to an existing value –Example: FontDialog1.Font = subTotalLabel.Font;

17 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 17 Creating Context Menus Context menus are the shortcut menus that pop up when you right-click on an object Items on a context menu are generally specific to that object Add a ContextMenu control to the form Click on words Context Menu then Type Here to add menu items

18 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 18 Creating Context Menus cont. An application can have more than one context menu Assign the context menu to a form or control with the ContextMenu property If there is only one context menu, attach it to the form

19 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 19 Creating a Context Menu – Step-by-Step You are going to create a context menu that contains these menu items: Color… Font… Exit… 1.Add the Context Menu to a Form 2.Test the Program

20 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 20 Writing General Methods A general method contains reusable code that is called from other methods The method may return a value or not –If a value is returned, specify the return type before the method name –If no value is returned, use the keyword void before the method name

21 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 21 Creating a New Method Write a method header and enclose the lines of code within a set of braces Example: private void SelectColor() { //Display the color dialog box ColorDialog1.ShowDialog(); }

22 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 22 Creating a New Method cont. You must specifically call the method from another method Example: private void changeButtonMessage_Click(object sender, System.EventArgs e) { //Change the color of the message SelectColor(); messageLabel.ForeColor = ColorDialog1.Color; }

23 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 23 Passing Arguments to Methods Use arguments when you need to use the value of a variable in one method and also in a second method that is called from the first Any arguments defined in a method must be supplied in a call to that method The argument value must be the same data type in both locations The name of an argument does not have to be the same in both locations

24 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 24 Writing Methods That Return Values Write a method that will calculate a value and return it to the place where the method was called The type of data returned is defined in the method header A method can return a value and use one or more arguments The name given an argument in the header is the name used to refer to that argument inside the method

25 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 25 Returning the Result of a Method The return value is passed back with a Return statement The keyword Return is followed by a variable or expression containing the value to return to the caller If the method header specifies void, no Return statement is required

26 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 26 Method Header Format Method nameArgument data type Return data typeArgument Access modifier private decimal Commission(decimal decSalesAmount)

27 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 27 Methods with Multiple Arguments The sequence and data type of the arguments in the call must exactly match the arguments in the method header The list of zero or more arguments is enclosed in parentheses When you call the method, the Visual Studio.NET smart editor displays the arguments of your method

28 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 28 Breaking Calculations into Smaller Units Projects with many calculations can be easier to understand and write if broken down into small units Each unit should perform one program function or block of logic

29 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 29 Your Hands-On Programming Example Modify the hands-on programming example from Chapter 4 by replacing some of the buttons with menus. Write a method to calculate the sales tax and allow the user to select the font and color of the summary labels.

30 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 30 Summary The Visual Studio Menu Designer enables you to create menus, menu items, and submenus, each with keyboard access keys. In the Menu Designer you can set and modify the order and level of menu items. Each menu item has a Click event. The code to handle selection of a menu item belongs in the item’s Click event-handling method.

31 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 31 Summary cont. Common dialog boxes allow C# programs to display the predefined Windows dialog boxes for Print, PrintPreview, File Open, File Save, Fonts, and Colors. Context menus, or shortcut menus, are created using a ContextMenu control and the Menu Designer. Context menus pop up when the user right-clicks.

32 © 2003 by The McGraw-Hill Companies, Inc. All rights reserved. 5- 32 Summary cont. The programmer can write reusable code in general methods. These methods can be called from any other method in the form class. Methods that return a value must specify the data type of the return value and set the value to return using a return statement. If the return type is void, no value can be returned.


Download ppt "Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved."

Similar presentations


Ads by Google