1 Tutorial 5 Completing the Inventory Application Introducing Programming 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
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.
5.1 Test-Driving the Inventory Application 3 Application Requirements A college bookstore receives cartons of textbooks. In each shipment, each carton contains the same number of textbooks. The inventory manager wants to use a computer to calculate the total number of textbooks arriving at the bookstore for each shipment, from the number of cartons and the number of textbooks in each carton. The inventory manager will enter the number of cartons received and the fixed number of textbooks in each carton for each shipment; then the application will calculate the total number of textbooks in a shipment.
4 5.1 Test-Driving the Inventory Application Figure 5.1 Inventory application with quantities entered. Input data into application Enter 3 in the Cartons per shipment: TextBox Enter 15 in the Items per carton: TextBox
5.1 Test-Driving the Inventory Application 5 Figure 5.2 application. Result of clicking the Calculate Total Button in the Inventory Result of calculation Click the Calculate Total Button Observe result of calculation in the descriptive Label
6 5.2 Introduction to C# Code The Options dialog Setting option for Visual Studio.NET environment
5.2 Introduction to C# Code 7 Figure 5.3 Options dialog. Text Editor folder icon
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
5.2 Introduction to C# Code 9 Figure 5.4 General settings page for C# text editor. C# folder General item Line Numbers CheckBox (checked)
10 5.2 Introduction to C# Code Setting the tab Change the Indenting option Change the Tabs settings Insert spaces for each tab
5.2 Introduction to C# Code 11 Figure 5.5 Setting the Tabs options. Smart Indenting (selected) Tabs item Insert spaces (selected)
12 5.2 Introduction to C# Code Fonts and colors Change color used to display code Use Defaults Button resets values
5.2 Introduction to C# Code 13 Figure 5.6 Examining the Fonts and Colors page. Fonts and Colors item Use Defaults Button
14 5.2 Introduction to C# Code Renaming the Form Set Form s Name property to FrmInventory
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
5.2 Introduction to C# Code 16 Figure 5.7 application. IDE showing the uppermost portion of the code for the Inventory Inventory.cs tabbed window Using FCL namespaces Class is declared in the Inventory namespace Class declaration
5.2 Introduction to C# Code 17 Figure 5.8 application. IDE showing the lowermost portion of the code for the Inventory Main method
18 5.2 Introduction to C# Code Generated code Expanding the code Outlined code
5.2 Introduction to C# Code 19 Figure 5.9 Windows Form Designer generated code, when expanded. Expanded code
5.2 Introduction to C# Code 20 Figure 5.10 Ellipses indicating outlined code. Outlined code
21 5.2 Introduction to C# Code Generated code for a control Default properties The result of changes from design view
5.2 Introduction to C# Code 22 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 5.3 Inserting an Event Handler Renaming the Form in code In comments above class declaration In Main method
5.3 Inserting an Event Handler 24 Figure 5.12 Renaming the Form above the class declaration.
5.3 Inserting an Event Handler 25 Figure 5.13 Renaming the Form in method Main and adding spacing.
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
5.3 Inserting an Event Handler 27 Figure 5.14 Event handler btncalculate_click before you add your code. Empty event handler Asterisks indicate unsaved changes to application
28 5.3 Inserting an Event Handler Figure 5.15 Running application without functionality. Close box Examine the application Closing an application using the close box
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 (.)
5.3 Inserting an Event Handler 30 Figure 5.16 Adding code to the Calculate Total Button s event handler. Event handler Type this code
31 5.3 Inserting an Event Handler IntelliSense Display entire class members and their purposes Assignment operator Two operands Right to left
5.3 Inserting an Event Handler 32 Figure 5.17 IntelliSense activating while entering code. Selected member Intellisense Description of selected member
33 5.3 Inserting an Event Handler Figure 5.18 Running the application with an event handler. Result of clicking Calculate Total Button Run the application Enter input and examine the result
5.4 Performing a Calculation and Displaying the Result 34 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
5.4 Performing a Calculation and Displaying the Result 35 Figure 5.19 Using multiplication in the Inventory application. Modified Inventory application code
1 using System; 2 using System.Drawing; 3 using System.Collections; 4 using System.ComponentModel; 5 using System.Windows.Forms; 6 using System.Data; 7 8 namespace Inventory 9 { 10 /// <summary> 11 /// Summary description for FrmInventory. 12 /// </summary> 13 public class FrmInventory : System.Windows.Forms.Form 14 { 15 private System.Windows.Forms.Label lblcartons; 16 private System.Windows.Forms.Label lblitems; 17 private System.Windows.Forms.Label lbltotal; 18 private System.Windows.Forms.Label lbltotalresult; 19 private System.Windows.Forms.TextBox txtcartons; 20 private System.Windows.Forms.TextBox txtitems; 21 private System.Windows.Forms.Button btncalculate; 22 /// <summary> 23 /// Required designer variable. 24 /// </summary> 25 private System.ComponentModel.Container components = null; Renaming the Form Outline Inventory.cs (1 of 4) 36
26 27 public FrmInventory() 28 { 29 // 30 // Required for Windows Form Designer support 31 // 32 InitializeComponent(); 33 34 // 35 // TODO: Add any constructor code after InitializeComponent 36 // call 37 // 38 } 39 40 /// <summary> 41 /// Clean up any resources being used. 42 /// </summary> 43 protected override void Dispose( bool disposing ) 44 { 45 if( disposing ) 46 { 47 if (components!= null) 48 { 49 components.dispose(); 50 } Outline Inventory.cs (2 of 4) 37
51 } 52 base.dispose( disposing ); 53 } 54 55 // Windows Form Designer generated code 56 57 /// <summary> 58 /// The main entry point for the application. 59 /// </summary> 60 [STAThread] 61 static void Main() 62 { 63 Application.Run( new FrmInventory() ); 64 } 65 66 // handles Calculate Button's Click event 67 private void btncalculate_click( 68 object sender, System.EventArgs e ) 69 { 70 // multiply values input and display result in Label 71 lbltotalresult.text = Convert.ToString( 72 Int32.Parse( txtcartons.text ) * 73 Int32.Parse( txtitems.text ) ); 74 75 } // end method btncalculate_click Renaming the Form Adding a comment to your code Adding a comment to your code Outline Inventory.cs (3 of 4) Adding an event handler to your code 38
76 77 } // end class FrmInventory 78 } Adding a comment to your code Outline 39 Inventory.cs (4 of 4)
40 5.5 Using the Debugger: Syntax Errors Figure 5.21 Results of successful build in the Output window. Output window Shows result of compilation
41 5.5 Using the Debugger: Syntax Errors Figure 5.22 Task List lists syntax errors. The Task List Displays the build error Description of the error Line number corresponding to the error
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
5.5 Using the Debugger: Syntax Errors 43 Figure 5.23 IDE with syntax errors. Jagged red underline indicates syntax error
5.5 Using the Debugger: Syntax Errors 44 Figure 5.24 Task List displaying the syntax error recognized in real time.
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
5.5 Using the Debugger: Syntax Errors 46 Figure 5.25 Highlighting the portion of code where a syntax error occurs. Highlighted syntax error
47 5.5 Using the Debugger: Syntax Errors Identifying second syntax error Build > Build Solution compiles code Syntax error not found in real time
5.5 Using the Debugger: Syntax Errors 48 Figure 5.26 IDE with second syntax error. Jagged blue underline indicates compilation error Compilation error shown in task list