Testing a Windows Forms Application (3 of 3)

From webpage

Testing a Windows Forms Application - Perspective


(Page 3 of 3 )

If you can code and test the Invoice Total project that's presented in this chapter, you've already learned a lot about Visual Basic 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. In particular, you haven't learned much about the Visual Basic language. That's why the next six chapters present the Visual Basic essentials.

Terms

object-oriented programming

syntax error

object-oriented language

build error

object

code snippet

class

identifier

instance

smart tag menu

instantiation

My feature

property

comment out a line

method

bookmark

event

collapse

member

expand

dot operator

build a project

dot

run a project

argument

test a project

event-driven application

runtime error

event handler

exception

procedure declaration

bug

procedure name

debug

tool tip

break mode

line-continuation character

data tip

comment

Exercise 3-1 Code 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. 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:\VB 2008\Chapter 02 directory to the C:\VB 2008\Chapter 03 directory.
  2. Open the Invoice Total solution (InvoiceTotal.sln) that's now in the C:\VB 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 procedure declaration for the Click event of this object. Then, enter the code for this procedure as shown in figure 3-6. As you enter the code, be sure to take advantage of all of theVisual Studio features for coding including snippets.
  4. Return to the Form Designer, and double-click the Exit button to generate the procedure 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-9. If any syntax errors are listed in this window, double-click on each error to move to the error in the Code Editor. If the Auto Correction feature is available for an error, check to see whether its suggested correction (or one of its suggested corrections) is the one you want to make. Then, correct the error.

    Test the application
  6. Press F5 to build and run the project. If any syntax errors are detected, you'll need to correct the errors and press F5 again.
  7. When the application runs and the Invoice Total form is displayed, 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 procedures 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.
  9. Note the highlighted statement and read the message that's displayed in the Exception Assistant. Then, close the Assistant, and move the mouse pointer over the property in this statement to display its data tip. This shows that the code for this application needs to be enhanced so it checks for invalid data.
  10. Display the smart tag for the Text property in the highlighted statement, click its drop-down arrow, and select Text Visualizer. This shows the data exactly as it was entered in the Text Visualizer dialog box. Then, click the Stop Debugging button in Standard toolbar to end the application.

    Experiment with the Visual Basic features
  11. In the Dim statement for the discountPercent variable, change the variable name to discountPct. When you do that, a bar will appear under the last letter of the variable. Place the mouse pointer over this bar to display a drop-down arrow. Then, click on this arrow and select the Rename command. This should rename the discountPercent variable to discountPct throughout the form. But run the form to make sure it's working correctly.
  12. In the If statement, right-click on one of the occurrences of the variable named discountPct. Then, select the Rename command, and use it to rename this variable to discountPercent throughout the form. To make sure this worked, run the application.
  13. Select the lines that contain the ElseIf clauses and click on the Comment Out button in the Standard toolbar. That should change these coding lines to comments. Then, run the application to see how it works when these lines are ignored. When you're done, select the lines that were commented out and click on the Uncomment button to restore them.
  14. In the Code Editor, click on the minus sign in front of the btnCalculate_Click procedure to collapse it. Then, expand that procedure and collapse the btnExit_Click procedure. Last, print just the expanded code for this form.
  15. In the Solution Explorer, show all the files and double-click on the file named frmInvoiceTotal.Designer.vb to open it in the Code Editor. This is the code that determines how the form will look when it's instantiated. After you read chapter 11 and section 4, this code will make more sense to you. For now, though, just close the window with this code.

    Experiment with the Help feature
  16. To see how context-sensitive help works, place the insertion point in the Select 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.
  17. In the left pane, select the Index tab to display the Index window. Type "snippets" into the Look For box to see the entries that are listed under this topic. Next, if Visual Basic (or Visual Basic Express Edition) isn't selected in the Filtered By drop-down list, select it to show just the topics for Visual Basic. Then, click on one or more topics to display them.
  18. Use the Tools->Options command, and review the help options. In the Online group, you may want to change the loading option to "Try local only, not online," because that can speed up the access of help topics.
  19. Continue to experiment with the Index, Contents, Help Favorites, and Search features to see how they work, and try to use some of the buttons in the Standard toolbar to see how they work. Then, close the Help window.

    Exit from Visual Studio
  20. 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!

No comments: