FREE BOOK

Chapter 3: How to code and test a Windows Forms application using C# 2008

Posted by Murach Free Book | Windows Forms December 10, 2008
In this chapter, you'll learn how to code and test a Windows Forms application. When you're done, you'll be able to develop simple applications of your own.

Perspective

If you can code and test the Invoice Total project that's presented in this chapter, you've already learned a lot about C# programming. You know how to enter the code for the event handlers that make the user interface work the way you want it to. You know how to build and test a project. And you know some simple debugging techniques.

On the other hand, you've still got a lot to learn. For starters, you need to learn the C# language. So in the next six chapters, you'll learn the essentials of the C# language. Then, in chapter 11, you'll learn some debugging techniques that can be used with more advanced code.

Terms



Exercise 3-1 Code and test the Invoice Total form


In this exercise, you'll add code to the Invoice Total form that you designed in exercise 2-1. Then, you'll build and test the project to be sure it works correctly. You'll also experiment with debugging and review some help information.

Copy and open the Invoice Total application

  1. Use the Windows Explorer to copy the Invoice Total project that you created for chapter 2 from the C:\C# 2008\Chapter 02 directory to the C:\C# 2008\Chapter 03 directory.

  2. Open the Invoice Total solution (InvoiceTotal.sln) that's now in the C:\C# 2008\Chapter 03\InvoiceTotal directory.

    Add code to the form and correct syntax errors

  3. Display the Invoice Total form in the Form Designer, and double-click on the Calculate button to open the Code Editor and generate the method declaration for the Click event of this object. Then, enter the code for this method as shown in figure 3-6. As you enter the code, be sure to take advantage of all of the Visual Studio features for coding including snippets.

  4. Return to the Form Designer, and double-click the Exit button to generate the method declaration for the Click event of this object. Enter the statement shown in figure 3-6 for this event handler.

  5. Open the Error List window as described in figure 3-7. If any syntax errors are listed in this window, double-click on each error to move to the error in the Code Editor. Then, correct the error.

    Test the application

  6. Press F5 to build and run the project. If you corrected all the syntax errors in step 5, the build should succeed and the Invoice Total form should appear. If not, you'll need to correct the errors and press F5 again.

  7. Enter a valid numeric value in the first text box and click the Calculate button or press the Enter key to activate this button. Assuming that the calculation works, click the Exit button or press the Esc key to end the application. If either of these methods doesn't work right, of course, you need to debug the problems and test the application again.

    Enter invalid data and display data tips in break mode

  8. Start the application again. This time, enter "xx" for the subtotal. Then, click the Calculate button. This will cause Visual Studio to enter break mode and display the Exception Assistant as shown in figure 3-15.

  9. Note the highlighted statement and read the message that's displayed in the Exception Assistant. Then, move the mouse pointer over the variable and property in this statement to display their data tips. This shows that the code for this application needs to be enhanced so it checks for invalid data. You'll learn how to do that in chapter 7. For now, though, click the Stop Debugging button in the Debug toolbar to end the application.

    Create a syntax error and see how it affects the IDE

  10. When you return to the Code Editor, hide the Error List window by clicking on its Auto Hide button. Next, change the name of the Subtotal text box from txtSubtotal to txtSubTotal. This creates an error since the capitalization doesn't match the capitalization used by the Name property of the text box.

  11. Try to run the application, and click No when Visual Studio tells you the build had errors and asks whether you want to continue with the last successful build. Then, double-click on the error in the Error List, correct the error, and run the application again to make sure the problem is fixed.

    Use refactoring

  12. Change the name of the subtotal variable from subtotal to invoiceSubtotal. When you do a bar will appear under the last letter of the variable. Point at this bar to display a drop-down arrow. Then, click on this arrow and select the Rename command. This should rename the subtotal variable throughout the form, but run the form to make sure it's working correctly.

    Generate and delete an event handler

  13. Display the Form Designer for the Invoice Total form and double-click a blank area on the form. This should generate an event handler for the Load event of the form.

  14. Delete the event handler for the Load event of the form. Then, run the application. When you do, you'll get a build error that indicates that the form does not contain a definition for this event handler.

  15. Double-click on the error. This opens the Designer.cs file for the form and jumps to the statement that wires the event handler. Delete this statement to correct the error.

  16. If you're curious, review the generated code that's stored in the Designer.cs file for this simple form. Then, click the minus sign to the left of the region named "Windows Form Designer generated code" to collapse this region.

  17. Run the form to make sure it's working correctly. When you return to the Code Editor, close the Designer.cs file for the form.

    Experiment with the Help feature

  18. To see how context-sensitive help works, place the insertion point in the Focus method in the last statement of the first event handler and press F1. This should open a Help window that tells you more about this method.

  19. In the left pane, select the Index tab to display the Index window. Type "snippets" into the Look For box in this window to see the entries that are listed under this topic. Next, if Visual C# (or Visual C# Express Edition) isn't selected in the Filter By drop-down list, select it to show just the topics for C#. Then, click on one or more topics to display them.

  20. Continue experimenting with the Index, Contents, Help Favorites, and Search features to see how they work, and try using some of the buttons in the Web toolbar to see how they work. Then, close the Help window.

    Exit from Visual Studio

  21. Click the Close button for the Visual Studio window to exit from this application. If you did everything and got your application to work right, you've come a long way!

Total Pages : 10 678910

comments