chapter 5 menus, common dialog boxes, and methods programming in c#.net © 2003 by the mcgraw-hill...

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

Upload: louise-phelps

Post on 30-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

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

Chapter 5 Menus, Common Dialog

Boxes, and Methods

Programming in C# .NET

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.

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

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.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

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

© 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

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

© 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

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

© 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

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

© 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

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

© 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

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

© 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:

File Help

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

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

© 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

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

© 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

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

© 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

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

© 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

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

© 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

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

© 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

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

© 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

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

© 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;

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

© 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

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

© 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

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

© 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 Form2. Test the Program

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

© 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

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

© 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();

}

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

© 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;

}

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

© 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

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

© 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

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

© 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

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

© 2003 by The McGraw-Hill Companies, Inc. All rights reserved.5- 26

Method Header Format

Method name Argument data type

Return data type Argument

Access modifier

private decimal Commission(decimal decSalesAmount)

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

© 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

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

© 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

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

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

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

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

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

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

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

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