Download presentation
Presentation is loading. Please wait.
Published byArabella Marshall Modified over 9 years ago
1
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application 5.2 Introduction to C# Code 5.3 Inserting an Event Handler 5.4 Performing a Calculation and Displaying the Result 5.5 Using the Debugger: Syntax Errors 5.6 Wrap-Up Tutorial 5 – Completing the Inventory Application Introducing Programming
2
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Add an event handler for a Button control. –Insert code into an event handler. –Access a property’s value by using C# code. –Use the assignment and multiplication operators.
3
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 5.1 Test-Driving the Inventory Application
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 5.1 Test-Driving the Inventory Application Input data into application –Enter 3 in the Cartons per shipment: TextBox –Enter 15 in the Items per carton: TextBox Figure 5.1 Inventory application with quantities entered.
5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 5.1 Test-Driving the Inventory Application Click the Calculate Total Button –Observe result of calculation in the descriptive Label Figure 5.2 Result of clicking the Calculate Total Button in the Inventory application. Result of calculation
6
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 5.2 Introduction to C# Code The Options dialog –Setting option for Visual Studio.NET environment
7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 5.2 Introduction to C# Code Figure 5.3 Options dialog. Text Editor folder icon
8
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 5.2 Introduction to C# Code Adding line numbers –Select the Text Editor folder icon Select the General item –Place a check in the CheckBox next to Line Numbers
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 5.2 Introduction to C# Code Figure 5.4 General settings page for C# text editor. C# folder General item Line Numbers CheckBox (checked)
10
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 5.2 Introduction to C# Code Setting the tab –Change the Indenting option –Change the Tabs settings –Insert spaces for each tab
11
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 5.2 Introduction to C# Code Figure 5.5 Setting the Tabs options. Smart Indenting (selected) Tabs item Insert spaces (selected)
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 5.2 Introduction to C# Code Fonts and colors –Change color used to display code –Use Defaults Button resets values
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 5.2 Introduction to C# Code Figure 5.6 Examining the Fonts and Colors page. Fonts and Colors item Use Defaults Button
14
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 5.2 Introduction to C# Code Renaming the Form –Set Form ’s Name property to FrmInventory
15
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 5.2 Introduction to C# Code The code editor –Code view using directive –Allows access to classes within specified namespace Class declaration –class keyword –Identifier –Braces around class body –Case sensitivity C# keywords and identifiers are case sensitive –Main method
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 5.2 Introduction to C# Code Figure 5.7 IDE showing the uppermost portion of the code for the Inventory application. Inventory.cs tabbed window Class declaration Class is declared in the Inventory namespace Using FCL namespaces
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 5.2 Introduction to C# Code Figure 5.8 IDE showing the lowermost portion of the code for the Inventory application. Main method
18
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 5.2 Introduction to C# Code Generated code –Expanding the code –Outlined code
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 5.2 Introduction to C# Code Figure 5.9 Windows Form Designer generated code, when expanded. Expanded code
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 5.2 Introduction to C# Code Figure 5.10 Ellipses indicating outlined code. Outlined code
21
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 5.2 Introduction to C# Code Generated code for a control –Default properties –The result of changes from design view
22
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 5.2 Introduction to C# Code Figure 5.11 Code generated by the IDE for lblCartons (with the code setting the Text property highlighted). Click tab for design view Property values for lblCartons
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 5.3 Inserting an Event Handler Renaming the Form in code –In comments above class declaration –In Main method
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 5.3 Inserting an Event Handler Figure 5.12 Renaming the Form above the class declaration.
25
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 5.3 Inserting an Event Handler Figure 5.13 Renaming the Form in method Main and adding spacing.
26
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 5.3 Inserting an Event Handler Event handling –Visual Studio.NET automatically inserts event handler when controls are double clicked –Event handler naming convention
27
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 5.3 Inserting an Event Handler Figure 5.14 Event handler btnCalculate_Click before you add your code. Asterisks indicate unsaved changes to application Empty event handler
28
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 5.3 Inserting an Event Handler Examine the application –Closing an application using the close box Figure 5.15 Running application without functionality. Close box
29
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 5.3 Inserting an Event Handler Adding code to an event handler –Comments in code Comments appear in green Use double slash characters –Executable C# statement Ends with semicolon Accessing properties –Member access operator (. )
30
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 5.3 Inserting an Event Handler Figure 5.16 Adding code to the Calculate Total Button ’s event handler. Event handler Type this code
31
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 5.3 Inserting an Event Handler IntelliSense –Display entire class’ members and their purposes Assignment operator –Two operands –Right to left
32
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 5.3 Inserting an Event Handler Figure 5.17 IntelliSense activating while entering code. Selected member Intellisense Description of selected member
33
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 5.3 Inserting an Event Handler Run the application –Enter input and examine the result Figure 5.18 Running the application with an event handler. Result of clicking Calculate Total Button
34
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 5.4 Performing a Calculation and Displaying the Result Changing the event handler –Add comments –Break header over multiple lines –Multiplication operator Int32.Parse method –Call a method –Return a value Convert.ToString method
35
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 5.4 Performing a Calculation and Displaying the Result Figure 5.19 Using multiplication in the Inventory application. Modified Inventory application code
36
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 Inventory.cs (1 of 4) Renaming the Form
37
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 Inventory.cs (2 of 4)
38
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 Inventory.cs (3 of 4) Renaming the Form Adding a comment to your code Adding an event handler to your code Adding a comment to your code
39
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 Inventory.cs (4 of 4) Adding a comment to your code
40
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 5.5 Using the Debugger: Syntax Errors Output window –Shows result of compilation Figure 5.21 Results of successful build in the Output window.
41
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 5.5 Using the Debugger: Syntax Errors The Task List –Displays the build error Description of the error Line number corresponding to the error Figure 5.22 Task List lists syntax errors.
42
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 5.5 Using the Debugger: Syntax Errors Identifying syntax error –Real-time error checking –Errors are underlined by a red jagged line –Task List will display the errors
43
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 5.5 Using the Debugger: Syntax Errors Figure 5.23 IDE with syntax errors. Jagged red underline indicates syntax error
44
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 5.5 Using the Debugger: Syntax Errors Figure 5.24 Task List displaying the syntax error recognized in real time.
45
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 45 5.5 Using the Debugger: Syntax Errors Matching the syntax error in the code –Double click an error in the Task List to highlight the code with the syntax error
46
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 46 5.5 Using the Debugger: Syntax Errors Figure 5.25 Highlighting the portion of code where a syntax error occurs. Highlighted syntax error
47
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 47 5.5 Using the Debugger: Syntax Errors Identifying second syntax error –Build > Build Solution compiles code Syntax error not found in real time
48
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 48 5.5 Using the Debugger: Syntax Errors Figure 5.26 IDE with second syntax error. Jagged blue underline indicates compilation error Compilation error shown in task list
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.