370 likes | 388 Views
Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 12 Testing, Testing …1, 2, 3. Objectives. Select appropriate test data for an application Prevent the entry of unwanted characters in a text box Create a message box with the MessageBox.Show method
E N D
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 12 Testing, Testing …1, 2, 3
Objectives • Select appropriate test data for an application • Prevent the entry of unwanted characters in a text box • Create a message box with the MessageBox.Show method • Trim leading and trailing spaces from a string Clearly Visual Basic: Programming with Visual Basic 2008
Will Your Application Pass the Test? • Invalid data • Data that the program is not expecting the user to enter • The result of the user making: • A typing error, entering the data in an incorrect format, or neglecting to make a required entry • Test with invalid data • To ensure that the program does not display erroneous results or end abruptly because of an input error Clearly Visual Basic: Programming with Visual Basic 2008
The Only Cookies – Version 1 Application • Figure 12-2 • Shows the interface for the Only Cookies-Version 1 application • The Only Cookies-Version 1 application • Expects the user to enter a number in the Pounds ordered text box • Figure 12-5 • Shows the application’s testing chart Clearly Visual Basic: Programming with Visual Basic 2008
Const intPRICE As Integer = 5 Dim decOrdered As Decimal Dim decTotalPrice As Decimal Decimal.TryParse(txtOrdered.Text, decOrdered) ' calculate and display total price decTotalPrice = decOrdered * intPRICE lblTotalPrice.Text = decTotalPrice.ToString("C2")
The Only Cookies – Version 2 Application Figure 12-7 Shows the interface for the Only Cookies-Version 2 application Figure 12-8 Shows the Calculate button’s Click event procedure Figure 12-9 Shows the testing chart for the Only Cookies-Version 2 application Clearly Visual Basic: Programming with Visual Basic 2008
Can only put in IntegersTry some of the Invalid one listed below Clearly Visual Basic: Programming with Visual Basic 2008
Stop! This is a Restricted Area! • KeyPress event • Occurs each time the user presses a key while the control has the focus • When the KeyPress event occurs: • A character corresponding to the pressed key is sent to the KeyPress event’s e parameter • To prevent a text box from accepting an inappropriate character: • First use the eparameter’s KeyChar property to determine the pressed key Clearly Visual Basic: Programming with Visual Basic 2008
Stop! This is a Restricted Area! (continued) • e parameter’s Handled property • Used to cancel the pressed key if it is an inappropriate one • ControlChars.Back constant • Used to refer to the Backspace key • Backspace key • Necessary for editing the text box entry Clearly Visual Basic: Programming with Visual Basic 2008
The Shady Hollow Hotel – Version 1 Application- Case Statement • Figure 12-13 • Shows the interface for the Shady Hollow Hotel-Version 1 application • Figure 12-14 • Shows the Click event procedure • Figure 12-15 • Shows the application’s testing chart Clearly Visual Basic: Programming with Visual Basic 2008
The Shady Hollow Hotel – Version 2 Application- If-Then Else Statement • Figure 12-17 • Shows the interface for the Shady Hollow Hotel-Version 2 application • Interface uses a text box for the room type selection • Figure 12-18 • Shows the Click event procedure • In this application: • User will be entering a string in the txtType control • According to third guideline: • You need to test each path in the selection structure Clearly Visual Basic: Programming with Visual Basic 2008
I Need To Tell You Something • Messages can be displayed • Either in a label control in the interface or in a message box • Figure 12-20 • Shows the basic syntax of the MessageBox.Show method Clearly Visual Basic: Programming with Visual Basic 2008
Just When You Thought It was Safe (continued) • Trim method • Used to remove leading and trailing spaces • Figure 12-26 • Shows the basic syntax of the Trim method • Includes examples of using the method Clearly Visual Basic: Programming with Visual Basic 2008
strType = txtType.Text.ToUpper.Trim If strType = "S" Then intDailyRate = 90 ElseIf strType = "D" Then intDailyRate = 115 Else MessageBox.Show("Please enter a valid room type", "Shady Hollow Hotel“, MessageBox.Buttons.OK, MessageBoxIcon.Information) txtType.Text = "" txtType.Focus() End If
Const intGOLD_CLUB As Integer = 10 Select Case True Case radStandard.Checked intDailyRate = 90 Case radDeluxe.Checked intDailyRate = 115 Case Else intDailyRate = 130 End Select ' subtract Gold Club discount If chkGoldClub.Checked = True Then intDailyRate = intDailyRate - intGOLD_CLUB End If
The only thing you can put in the textbox is an s, S, d, D or a Backspace Private Sub txtType_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtType.KeyPress ' allows the text box to accept only the letters ' S, s, D, and d and the Backspace key for editing If e.KeyChar <> "S" AndAlso e.KeyChar <> "s" _ AndAlso e.KeyChar <> "D" AndAlso e.KeyChar <> "d" _ AndAlso e.KeyChar <> ControlChars.Back Then e.Handled = True End If End Sub
Can’t put anything in except S, s, D, d and the backspace Key Private Sub txtType_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtType.KeyPress ' allows the text box to accept only the letters ' S, s, D, and d and the Backspace key for editing If e.KeyChar <> "S" AndAlso e.KeyChar <> "s" _ AndAlso e.KeyChar <> "D" AndAlso e.KeyChar <> "d" _ AndAlso e.KeyChar <> ControlChars.Back Then e.Handled = True End If End Sub
Just When You Thought It Was Safe • Bugs • Errors in the application • Number of bugs • Directly related to the size and complexity of the application • Beta testers • Encouraged to use the application as often as possible, because some bugs surface only after months of use Clearly Visual Basic: Programming with Visual Basic 2008
Summary • Thoroughly test programs • Before releasing them to the public • Figure 12-1 • Lists the guidelines for selecting test data • You can use a testing chart to: • Record the test data and the expected results • To prevent a text box from accepting a character: • Code the text box’sKeyPress event procedure Clearly Visual Basic: Programming with Visual Basic 2008
Summary (continued) • Whenever you make a change to an application’s code: • Retest the application using the data listed in its testing chart • You can use the MessageBox.Show method to: • Display a message to the user while an application is running • Trim method • Makes a temporary copy of a string • Then performs the necessary trimming on the copy only Clearly Visual Basic: Programming with Visual Basic 2008