Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic Outline 6.1 Test-Driving the Enhanced Inventory Application 6.2 Variables 6.3 Handling the TextChanged Event 6.4 Memory Concepts 6.5 Arithmetic 1
2 Objectives In this tutorial, you will learn to: Create variables. Handle the TextChanged event. Apply basic memory concepts using variables. Use the precedence rules of arithmetic operators. Set breakpoints to debug applications.
6.1 Test-Driving the Enhanced Inventory Application 3 Application Requirements The inventory manager notices a flaw in your Inventory application. Although the application calculates the correct result, that result continues to display even after new data is entered. The only time the output changes is when the inventory manager clicks the Calculate Button again. You need to alter the Inventory application to clear the result as soon as the user enters new information into either of the TextBoxes, to avoid any confusion over the accuracy of your calculated result.
6.1 Test-Driving the Enhanced Inventory Application 4 Figure 6.1 Inventory application GUI displayed when the application is executed. Running the application
6.1 Test-Driving the Enhanced Inventory Application 5 Figure 6.2 Running the Inventory application. Using the application Enter data into the TextBoxes and click the Calculate Total Button Notice the result
6.1 Test-Driving the Enhanced Inventory Application 6 Figure 6.3 Modified Inventory application clears output Label after new input. Cleared output Label Notice the new feature The output Label is cleared when new data is entered
7 6.2 Variables Adding code to the application Rearranging control declarations Declaring a variable Data types Built-in data type
6.2 Variables 8 Figure 6.4 The unarranged control declarations
6.2 Variables 9 Figure 6.5 Rearranging and commenting the control declarations
6.2 Variables 10 Figure 6.6 Declaring variables in event handler btncalculate_click Click event handler Variable declarations
11 6.2 Variables Retrieving data from the user Data is stored in the TextBox s Text property The Int32.Parse function Converts a string to an int Explicit conversion Assignment operator
6.2 Variables 12 Figure 6.7 Retrieving numerical input from TextBoxes. Assigning user input to variables
6.2 Variables 13 Built-in (primitive) data types bool char float long short byte decimal int ulong ushort sbyte double uint object string Figure 6.8 C# built-in data types.
14 6.2 Variables Perform the multiplication Displaying the result Set the Label s Text property to the multiplication result
6.2 Variables 15 Figure 6.9 Multiplication using variables in btncalculate_click. Calculating and displaying the result
16 6.2 Variables Figure 6.10 Displaying the multiplication result using variables. Result of calculation Running the application Enter data Click the Calculate Total Button Notice the result
17 6.3 Handling the TextChanged Event Create the event handler for the first TextBox Double click the TextBox TextChanged event handler is created Clearing the value Use empty string Repeat for second TextBox
6.3 Handling the TextChanged Event 18 Figure 6.11 TextChanged event handler for Cartons per shipment: TextBox. TextChanged event handler
6.3 Handling the TextChanged Event 19 Figure 6.12 TextChanged event handler for Items per carton: TextBox.
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 // Label and TextBox display the number of cartons per shipment 16 private System.Windows.Forms.Label lblcartons; 17 private System.Windows.Forms.TextBox txtcartons; 18 19 // Label and TextBox display the number of items per carton 20 private System.Windows.Forms.Label lblitems; 21 private System.Windows.Forms.TextBox txtitems; 22 23 // Labels display the total value 24 private System.Windows.Forms.Label lbltotal; 25 private System.Windows.Forms.Label lbltotalresult; Outline Inventory.cs (1 of 5) Rearranging and commenting the control declarations 20
26 27 // Button calculates total value 28 private System.Windows.Forms.Button btncalculate; 29 30 /// <summary> 31 /// Required designer variable. Outline Inventory.cs (2 of 5) 21 32 /// </summary> 33 private System.ComponentModel.Container components = null; 34 35 public FrmInventory() 36 { 37 // 38 // Required for Windows Form Designer support 39 // 40 InitializeComponent(); 41 42 // 43 // TODO: Add any constructor code after InitializeComponent 44 // call 45 // 46 } 47 48 /// <summary> 49 /// Clean up any resources being used. 50 /// </summary>
51 protected override void Dispose( bool disposing ) 52 { 53 if( disposing ) 54 { 55 if (components!= null) 56 { 57 components.dispose(); 58 } 59 } 60 base.dispose( disposing ); 61 } 62 63 // Windows Form Designer generated code 64 65 /// <summary> 66 /// The main entry point for the application. 67 /// </summary> 68 [STAThread] 69 static void Main() 70 { 71 Application.Run( new FrmInventory() ); 72 } 73 Outline Inventory.cs (3 of 5) 22
74 // handles Click event 75 private void btncalculate_click( 76 object sender, System.EventArgs e ) 77 { 78 // declare variables 79 int intcartons; 80 int intitems; 81 int intresult; 82 83 // retrieve numbers from TextBoxes 84 intcartons = Int32.Parse( txtcartons.text ); 85 intitems = Int32.Parse( txtitems.text ); 86 87 // multiply two numbers 88 intresult = intcartons * intitems; 89 90 // display result in Label 91 lbltotalresult.text = Convert.ToString( intresult ); 92 93 } // end method btncalculate_click 94 Outline Inventory.cs (4 of 5) Assigning a property s value to a variable Assigning a variable to a property 23
95 // handles TextChanged event for txtcartons 96 private void txtcartons_textchanged( 97 object sender, System.EventArgs e ) 98 { 99 lbltotalresult.text = ""; // clear output Label 100 101 } // end method txtcartons_textchanged 102 103 // handles TextChanged event for txtitems 104 private void txtitems_textchanged( 105 object sender, System.EventArgs e ) 106 { 107 lbltotalresult.text = ""; // clear output Label 108 109 } // end method txtitems_textchanged 110 111 } // end class FrmInventory 112 } Outline Defining a TextBox s TextChanged event Inventory.cs handler (5 of 5) Setting a TextBox s Text property to an empty string 24
25 6.4 Memory Concepts Figure 6.14 Memory location showing name and value of variable intcartons. intcartons 12 A simple variable Corresponds to actual location in the computer s memory Name Type Size Value
6.4 Memory Concepts 26 Figure 6.15 Memory locations after values for variables intcartons and intitems have been input. intcartons 12 intitems 10 Visualizing data The value of each variable is stored in a separate memory location
27 6.4 Memory Concepts Figure 6.16 Memory locations after a multiplication operation. intcartons 12 intitems 10 intresult 120 Using the variable s data Nondestructive when value is read from memory
28 Operator precedence 6.5 Arithmetic Operators in expressions contained within a pair of parentheses Unary positive and negative Multiplication and division operations Modulus (remainder) operations Addition and subtraction operations
6.5 Arithmetic 29 C# operation Arithmetic Algebraic expression C# expression operator Addition + f + 7 f + 7 Subtraction p c p c Multiplication * bm b * m Division / x / y or o x ȳ - r x y x / y Modulus (Remainder) % r modulo s r % s Unary Negative - e e Unary Positive + +g +g Figure 6.17 Arithmetic operators.