CIS162AD - C# Methods, Menus, and Dialog Boxes 05_methods_menus.ppt.

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

The Web Warrior Guide to Web Design Technologies
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Dialog Boxes and Menus. Menus Menu Bar Contains menus which drop down to display list of menu items Each item has a name and text property Each item has.
CIS162AD Inheritance 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Constructors & Inheritance.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
C# Programming: From Problem Analysis to Program Design1 9 Programming Based on Events.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
C# Programming: From Problem Analysis to Program Design1 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3 rd Edition.
Programming Based on Events
Guide To UNIX Using Linux Third Edition
1 Windows Printing. 2 Objectives You will be able to Output text and graphics to a printer. Print multipage documents. Use the standard Windows print.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Visual Basic Chapter 1 Mr. Wangler.
Variables and Constants
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CIS162AD - C# Decision Statements 04_decisions.ppt.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Input, Output, and Processing
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
CIS162AD - C# If Statements 04_decisions.ppt. CIS162AD2 Overview of Topic  Pseudocode  Flow Control Structures  Flowcharting  If, If-Else  Nested.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Introduction to Programming with RAPTOR
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1 CS161 Introduction to Computer Science Topic #9.
JavaScript, Fourth Edition
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
CIS162AD Inheritance Part 3 09_inheritance.ppt. CIS162AD2 Overview of Topics  Inheritance  Virtual Methods used for Overriding  Abstract Classes and.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Programming with Microsoft Visual Basic th Edition
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
Controlling Program Flow with Decision Structures.
CIS162AD – C# Call-by-Reference Methods 06_methods_by_ref.ppt.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Programming Fundamentals Enumerations and Functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Object-Oriented Programming Part 1 07_classes.ppt
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Variables and Arithmetic Operations
Lesson #6 Modular Programming and Functions.
Brief description on how to navigate within this presentation (ppt)
Presentation transcript:

CIS162AD - C# Methods, Menus, and Dialog Boxes 05_methods_menus.ppt

CIS162AD2 Overview of Topics  Top-Down Design  Built-in Methods  Black Box Analogy  Programmer-Defined Methods  Method’s Signature & Overloading  Pass-by-value Arguments  Menu Designer  Context Menus (right-click)  Common Dialog Boxes

CIS162AD3 Top-Down Design  A design method where the major task to be accomplished is divided into subtasks. –Divide-and-Conquer –Stepwise Refinement  The goal is to write a modular program that is easier to read and maintain.

CIS162AD4 Top-Down Design - Analogy  A design method where the major task to be accomplished is divided into subtasks. –Major Task: clean house –Subtasks: dust, vacuum, sweep, mop  Programming example. –Major Task: CS5 Sales Calculator –Subtasks: input qty, price calculate sales tax, shipping, subtotal, etc display total bill

CIS162AD5 Subtasks  Each subtask should perform a single well-defined task.  Each subtask may produce some result.  Treat them as small programs. Input -> Process -> Output  These subtasks can be used at different times in one program or placed in a library and used by many different programs.  The int.Parse method is a complicated task that converts strings to numbres, and is used by many different programs.

CIS162AD6 Top-Down Design Implementation  Top-Down design is implemented using methods.  A method is a collection of statements that are grouped together to perform a specific operation.  Methods have inputs called arguments.  Methods can be defined to return no value, but if it does return a value, only one value can be returned through the return statement.  Methods can “return” more than one value through call-by-reference arguments (more on this later).

CIS162AD7 Programmer-Defined Methods  Before coding some programmer-defined methods, let’s look at some built-in methods a little closer.  It will help understand what we are trying to develop if we look at how methods are used.

CIS162AD8 Built-in Methods  They are like small programs.  They have their own input – process – output.  They can have one or more inputs.  But methods can only return one output.  As programmers, we don’t get to see the code (process) of built-in methods.  The process is like a black box…

CIS162AD9 Black Box Analogy  Many methods are designed as black boxes.  This means that as programmers we should not be concerned how the methods performs its processing.  We only need to know the inputs, outputs, and what it does.  We know what is required to use it, but we don’t know how it does its calculations.  Develop your own methods with the Black Box Analogy in mind.

CIS162AD10 Convert Text to Integer – int.Parse  Convert Text to Integer method is named int.Parse.  The documentation for int.Parse states the value that will be returned, method name, and the type and number of arguments. intNumber = int.Parse(String) returnedValue = MethodName (arguments)  Int.Parse takes one argument that is a String.  The method is called using the following syntax: If txtQuantity.Text contains “3” then the function call: intNumber = int.Parse(txtQuantity.Text) would return a value of 3 into intNumber.

CIS162AD11 Method Call  We issue a method call when we use it.  Processing control is passed to the method until it completes its task and returns control back to the calling method.  Most methods return a value back, but only one value can be returned through the return statement.

CIS162AD12 Round Method  Methods can only return one value, but may have more than one argument. decWholeNum = decimal.Round(decNum, 0);  The value returned is a whole number with no digits after the decimal point.

CIS162AD13 Method’s Signature & Overloading  Method overloading occurs when more than one method with the same name is defined, but differ in the parameter list. MessageBox.Show(TextMessage); MessageBox.Show(TextMessage, TitleBar); MessageBox.Show(TextMessage, TitleBar, ButtonType); MessageBox.Show(TextMessage, TitleBar, ButtonsType, Icon);  Each method’s signature is unique and consists of: –Method name –The number, data types, and order of its parameters –Parameters can also be modified with the keywords ref and out (These keywords are covered in next powerpoint presentation) –The signature does NOT include the data type of the returned value and the names used to identify the parameters.

CIS162AD14 Small Programs  Think of each method as a small program with input, process, and output steps. Output < Process < Input returnedValue = MethodName (arguments) intNumber = int.Parse(String)

CIS162AD15 Programmer-Defined Methods  The single value that a method returns can be a string or numeric.  Two Components 1. Method Definition 2. Method Call

CIS162AD16 Function Definition - Syntax  Syntax: private dataType MethodName(arguments) { statement(s) return expression; }  dataType is where we specify what type of value is being returned, string or numeric.  Use the return statement to return the value.  Variables that will be used to store the values sent into the method are listed in parentheses as arguments.

CIS162AD17 Method Definition - Example private decimal calcExtendedPrice(int intQty, decimal decPrice) { decimal decExtendedPrice; decExtendedPrice = intQty * decPrice; return decExtendedPrice; }  decimal is the data type of the value being returned.  calcExtendedPrice is the Method Name.  intQty and decPrice are the arguments passed to method.  The signature of this method is calcExtendedPrice(int, decimal)

CIS162AD18 Return Statement  Return statement returns the value back to the variable listed to the left of the equal sign on the call statement.  Return statement returns processing control back to the calling procedure.  Only one value can be returned.  Variable data type of returned value must match the return type specified in heading.

CIS162AD19 Multiple Returns  A method may be defined with multiple Return statements.  The first Return statement executed returns the value and returns processing control back to the calling procedure. private decimal calcExtendedPrice(int intQty, decimal decPrice { if (intQty < 26) return intQty * decPrice; else return intQty * (decPrice *.95);//5% discount }

CIS162AD20 Single Return Preferred  Although methods can be coded using multiple Return statements, one Return statement is preferred.  Having multiple Returns in complex procedure can be difficult to maintain and debug. private decimal calcExtendedPrice(int intQty, decimal decPrice ) { decimal decExtendedPrice; if (intQty < 26) decExtendedPrice = intQty * decPrice; else decExtendedPrice = intQty * (decPrice *.95); // 5% discount return decExtendedPrice; }

CIS162AD21 A Method without a Return  If a method that is supposed to return a value does not contain a return statement, then the compiler will generate a syntax error. private decimal calcExtendedPrice(int intQty, decimal decPrice ) { decimal decExtendedPrice; if (intQty < 26) decExtendedPrice = intQty * decPrice; else decExtendedPrice = intQty * (decPrice *.95); //return decExtendedPrice; //commented out – syntax error }

CIS162AD22 Method Call  The method call is issued when the method is used in another method. private void btnCalculate_Click (…) { intQty = int.Parse(txtQuantity.Text); decPrice = decimal.Parse(txtPrice.Text); decExtendedPrice = calcExtendedPrice(intQty, decPrice); }

CIS162AD23 Local Variables  Variables are local to the method in which they are defined.  Variables defined in a particular method are assigned their own memory and can only be referenced in that method.  Variables defined in calcExtendedPrice( ) are assigned their own memory and can only be referenced in calcExtendedPrice( ).  Different methods cannot see or reference each others variables.  They have separate memory allocations even though the variable names may be the same.

CIS162AD24 Passing Arguments private void btnCalculate_Click (…) { int intQty; decimal decPrice, decExtendedPrice; intQty = int.Parse(txtQuantity.Text); decPrice = decimal.Parse(txtPrice.Text); decExtendedPrice = calcExtendedPrice(intQty, decPrice); } private decimal calcExtendedPrice(int intQty, decimal decPrice) { decimal decExtended; decExtended = intQty * decPrice; return decExtended; }

CIS162AD25 Declare Variables ProcedureAddressVariableValue btnCalculate ( ) intQty decPrice decExtendedPrice calcExtendedPrice intQty decPrice decExtended

CIS162AD26 Input Values ProcedureAddressVariableValue btnCalculate( ) intQty decPrice decExtendedPrice calcExendedPrice intQty decPrice decExtended

CIS162AD27 decExtendedPrice = calcExtendedPrice(intQty, decPrice); Pass-by-Value - Values in variables of btnCalculate are sent to variables of calcExtendedPrice. ProcedureAddressVariableValue btnCalculate( ) intQty decPrice decExtendedPrice calcExtendedPrice intQty decPrice decExtended

CIS162AD28 Methods Use Local Variables private void btnCalculate_Click (…) { int intQty; decimal decPrice, decExtendedPrice; intQty = int.Parse(txtQuantity.Text); decPrice = decimal.Parse(txtPrice.Text); decExtendedPrice = calcExtendedPrice(intQty, decPrice); } private decimal calcExtendedPrice(int intQty, decimal decPrice) { decimal decExtended; decExtended = intQty * decPrice; return decExtended; }

CIS162AD29 decExtended = intQty * decPrice; ProcedureAddressVariableValue btnCalculate( ) intQty decPrice decExtendedPrice calcExtendedPrice intQty decPrice decExtended

CIS162AD30 Returning Method Values private void btnCalculate_Click (…) { int intQty; decimal decPrice, decExtendedPrice; intQty = int.Parse(txtQuantity.Text); decPrice = decimal.Parse(txtPrice.Text); decExtendedPrice = calcExtendedPrice(intQty, decPrice); } private decimal calcExtendedPrice(int intQty, decimal decPrice) { decimal decExtended; decExtended = intQty * decPrice; return decExtended; }

CIS162AD31 decExtendedPrice = calcExtendedPrice(intQty, decPrice) Return statements sends the value to variable on the left side of the equal sign on the method call statement. ProcedureAddressVariableValue btnCalculate( ) intQty decPrice decExtendedPrice calcExtendedPrice intQty decPrice decExtended

CIS162AD32 Menu Designer  Plan menu options / paths.  Add MenuStrip control to component tray. Just like the ToolTip.  Enter Text for menu item in box labeled Type Here.  Use ampersands to define control keys.  Name menu items using menu path and mnu prefix.  Need to know name so we can tell which menu item was selected in the code. private void mnuFileExit_Click(object sender, System.EventArgs e) { this.Close(); }

CIS162AD33 Menu Designer Example

CIS162AD34 Context Menus  Context menus are shortcut menus that pop up with a right-click.  Add a ContextMenuStrip control to the component tray.  Use the Menu Designer to add items.  More than one context menu can be added.  In order to make the context menu available to users, it must be assigned to the form or control by using the ContextMenuStrip property.  See the images on the next two slides…

CIS162AD35 Context Menu Example

CIS162AD36 Assign Context Menu to Form

CIS162AD37 Common Dialog Boxes  Common dialog boxes available: –FontDialog –ColorDialog –OpenFileDialog –SaveFileDialog –PrintDialog –PrintPreviewDialog

CIS162AD38 Common Dialog Boxes  The Common Dialog Box controls can save programmers the time and trouble of designing custom dialog boxes for common purposes.  Add a common dialog box control to the component tray.  The default Name of the Dialog maybe used, unless more than one is being used in the program.. colorDialog1, fontDialog, etc.  You won’t see anything in design time, because the dialog box is displayed at run-time with code.  Use the ShowDialog method to display a dialog box. fontDialog1.ShowDialog( ); colorDialog1.ShowDialog( );

CIS162AD39 FontDialog Box

CIS162AD40 Using Selected Options  Dialog Boxes are objects themselves with properties.  The values selected by users in a font or color dialog box are stored in the dialog box’s properties.  The values in the dialog boxes selected by users need to be applied to controls on the form. lblGrandTotal.Font = fontDialog1.Font; lblGrandTotalForeColor = colorDialog1.Color; Note: property on Labels and Textboxes is ForeColor.  Before showing a dialog box, you should assign to it the current value selected just incase users click on Cancel. fontDialog1.Font = lblGrandTotal.Font;

CIS162AD41 Method Example private void mnuEditFont_Click(object sender, System.EventArgs e) { //Initialize the font in the dialog box to the current font fontDialog1.Font = lblGrandTotal.Font; //Display dialog box so user can pick a font fontDialog1.ShowDialog(); //Apply the font chosen to the Totals section lblGrandTotal.Font = fontDialog1.Font; lblNumberOfOrders.Font = fontDialog1.Font; lblAverageOrder.Font = fontDialog1.Font; }

CIS162AD42 Summary  Top-Down Design  Built-in Methods  Black Box Analogy  Programmer-Defined Methods  Method’s Signature & Overloading  Menu Designer  Context Menus  Common Dialog Boxes