🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Chapter03.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

PART ONE An Introduction To Visual Studio © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C1, Slide 1 Chapter 3...

PART ONE An Introduction To Visual Studio © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C1, Slide 1 Chapter 3 How to code and test a Windows Forms application © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 2 Objectives Applied 1. Given the code for a simple application, use the skills presented in this chapter to add the code and test the application. 2. Use indentation and blank lines to make the code easier to read. 3. Use comments to document the code for the entire form or for portions of the code. 4. Use any of the help features to get the information that you need for developing an application. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 3 Objectives (cont.) Knowledge 1. In the context of object-oriented programming, describe these terms: class, object, instantiation, instance, property, method, event, and member. 2. Describe how an application responds to events. 3. Describe the use of snippets, refactoring, and annotations. 4. Describe the use of annotations in the vertical scroll bar of the Code Editor. 5. Distinguish between a syntax (or build) error and a runtime error. 6. Distinguish between testing and debugging. 7. Explain how a data tip can help debug a runtime error. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 4 1) Class and object concepts An object is a self-contained unit like a form or control that combines code and data. A class is the code that defines the characteristics of an object. You can think of a class as a template for an object. An object is an instance of a class, and the process of creating an object from a class is called instantiation. More than one object instance can be created from a single class. For example, a form can have several button objects, all instantiated from the same Button class. Each is a separate object, but all share the characteristics of the Button class. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 5 2) Property, method, and event concepts Properties define the characteristics of an object and the data associated with an object. Methods are the operations that an object can perform. Events are signals sent by an object to the application telling it that something has happened that can be responded to. Properties, methods, and events can be referred to as members of an object. If you instantiate two or more instances of the same class, all of the objects have the same properties, methods, and events. However, the values assigned to the properties can vary from one instance to another. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 6 3) Objects and forms When you use the Form Designer, Visual Studio automatically generates C# code that creates a new class based on the Form class. Then, when you run the project, a form object is instantiated from the new class. When you add a control to a form, Visual Studio automatically generates C# code in the class for the form that instantiates a control object from the appropriate class and sets the control’s default properties. When you move and size a control, Visual Studio automatically sets the properties that specify the location and size of the control. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 7 4) A member list in the Code Editor window © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 8 4) A member list in the Code Editor window… How to enter member names when working in the Code Editor 1. To display a list of the available members for a class or an object, type the class or object name followed by a period (called a dot operator, or just dot). 2. Type one or more letters of the member name, and the Code Editor will select the first entry in the list that matches those letters. Or, scroll down the list to select the member you want. 3. Press the Tab or Enter key to insert the member into your code. Note If a member list isn’t displayed, select the Tools→Options command to display the Options dialog box. Then, expand the Text Editor group, select the C# category, and check the Auto List Members and Parameters Information boxes. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 9 4) A member list in the Code Editor window… The syntax for referring to a member of a class or object ClassName.MemberName objectName.MemberName Statements that refer to properties txtTotal.Text = "10"; Assigns a string holding the number 10 to the Text property of the text box named txtTotal. txtTotal.ReadOnly = true; Assigns the true value to the ReadOnly property of the text box named txtTotal so the user can’t change its contents. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 10 4) A member list in the Code Editor window… Statements that refer to methods txtMonthlyInvestment.Focus(); Uses the Focus method to move the focus to the text box named txtMonthlyInvestment. this.Close(); Uses the Close method to close the form that contains the statement. Here, this is a keyword that is used to refer to the current instance of the class. Code that refers to an event btnExit.Click Refers to the Click event of a button named btnExit. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 11 5) How an application responds to events Windows Forms applications work by responding to events that occur on objects. To indicate how an application should respond to an event, you code an event handler, which is a special type of method that handles the event. To connect the event handler to the event, Visual Studio automatically generates a statement that wires the event to the event handler. This is known as event wiring. An event can be an action that’s initiated by the user like the Click event, or it can be an action initiated by program code like the Closed event. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 12 5) How an application responds to events… Common control events Event Occurs when… Click …the user clicks the control. DoubleClick …the user double-clicks the control. Enter …the focus is moved to the control. Leave …the focus is moved from the control. Common form events Event Occurs when… Load …the form is loaded into memory. Closing …the form is closing. Closed …the form is closed. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 13 6) Example-Exit button Event & Response ❑ Event: The user clicks the Exit button ❑ Response: The method for the Click event of the Exit button is executed private void btnExit_Click(object sender, EventArgs e) { this.Close(); } © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 14 6) Example-Exit button Event & Response… ❑ How to handle the Click event of a button 1. In the Form Designer, double-click the control. This opens the Code Editor, generates the declaration for the method that handles the event, and places the cursor within this declaration. 2. Type the C# code between the opening brace ({) and the closing brace (}) of the method declaration. 3. When you are finished writing code, you can return to the Form Designer by clicking on its tab. ❑ How to handle the Load event for a form Follow the procedure above, but double-click the form itself. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 15 7) Example-The event handlers for the Invoice Total form private void btnCalculate_Click(object sender, EventArgs e) { decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); decimal discountPercent = 0m; if (subtotal >= 500) { discountPercent =.2m; } else if (subtotal >= 250 && subtotal < 500) { discountPercent =.15m; } else if (subtotal >= 100 && subtotal < 250) { discountPercent =.1m; } decimal discountAmount = subtotal * discountPercent; decimal invoiceTotal = subtotal - discountAmount; txtDiscountPercent.Text = discountPercent.ToString("p1"); txtDiscountAmount.Text = discountAmount.ToString("c"); txtTotal.Text = invoiceTotal.ToString("c"); txtSubtotal.Focus(); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 16 8) Coding rules Use spaces to separate the words in each statement. Use exact capitalization for all keywords, class names, object names, variable names, etc. End each statement with a semicolon. Each block of code must be enclosed in braces ({}). That includes the block of code that defines the body of a method. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 17 9) Coding recommendations Use indentation and extra spaces to align statements and blocks of code so they reflect the structure of the program. Use spaces to separate the words, operators, and values in each statement. Use blank lines before and after groups of related statements. Note As you enter code in the Code Editor, Visual Studio automatically adjusts its formatting by default. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 18 9) Coding recommendations… ❑ How to code comments Comments are used to help document what a program does and what the code within it does. To code a single-line comment, type // before the comment. To code a delimited comment, type at the end. private void btnCalculate_Click(object sender, EventArgs e) { // get the subtotal amount from the Subtotal text box decimal subtotal = Convert.ToDecimal(txtSubtotal.Text); © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 19 10) How to test a project 1. Test the user interface, including the appearance of the controls, the tab order, the access keys, and the Enter and Esc keys. 2. Test valid input data. 3. Test invalid data or unexpected user actions. © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 20 11) How a project looks in break mode © 2016, Mike Murach & Associates, Inc. Murach's C# 2015 C3, Slide 21

Use Quizgecko on...
Browser
Browser