with exception handling...15. exception handling (try/catch) put all of the input/process/output...
TRANSCRIPT
8/8/2015
1
VISUAL BASIC LAB
Music and Videos
with Exception Handling
Copyright © 2015 Dan McElroy
Music and Videos - Lab Assignment Develop a Visual Basic application program that is used to input the number of music songs and the number of videos sold. The songs sell for $0.99 each and the videos sell for $4.95 each. The store keeps a 20% commission for each song and a 25% commission for each video. The program is to display the total number of items sold, the gross sales (total price), the amount of commission collected and the net amount given to the sellers. The program needs to reject invalid inputs and negative numbers.
A button needs to be provided to clear the inputs and display, and a button needs to be provided to exit the program.
8/8/2015
2
Define the Inputs and Outputs INPUT PROCESSING OUTPUT
Music Sold Videos Sold Compute button Clear button Exit button
Input Music and Videos sold Compute gross sales Compute commission Compute net to sellers Clear inputs and outputs Exit program
Total items sold Total gross sales Total commission Net to sellers
Music and Videos 1. Create the Form
Refer to the previous lab assignments on how to create a Visual Basic project.
Set the form's Text property to Music and Videos. This text will be displayed on the forms Title Bar.
8/8/2015
3
Music and Videos 2. Put a Title on the Form
Use a Label to put a title on the form.
Set the Text property to display your name and "Online Store"
Set the Name property to lblTitle (You may need to scroll to the top of the properties to see the Name property)
Music and Videos 3. Set the Title Font to 16pt, Bold
Make sure that the Label is selected, then click the square button with the … inside to open the font dialog box.
Adjust the size of the form if the label is too big for the form.
8/8/2015
4
Music and Videos 4. Music & Videos Sold GroupBox
Find GroupBox in the Containers section of the ToolBox. Click the arrow to expand the list.
Put a GroupBox on the form.
Set the Text property to Music && Video Sales
Because the ampersand & is a control character, use two ampersands && to display one &.
Music and Videos 5. Labels and TextBoxes for Input
Make sure that the GroupBox is still selected then add a Label and a TextBox to be used for inputting the number of music songs and videos sold. Set the Text property of the labels to Music and Videos
Name these controls: lblMusic txtMusic lblVideos txtVideos
8/8/2015
5
Music and Videos 6. Labels for Output
Make sure that the GroupBox is still selected then add a Labels for outputting the results. Set the Name and Text properties as follows:
Adjust the size of the form if needed.
Name Text
lblItemsSold Items Sold
lblGrossSales Gross Sales
lblCommission Commission
lblNetToSellers Net to Sellers
Music and Videos 7. Buttons for Compute, Clear and Exit
Make sure that the GroupBox is still selected then add the buttons. Adjust the size of the form if needed.
The ampersand & defines a hot key: Alt-C = Compute Alt-L = Clear Alt-X = Exit
Name Text
btnCompute &Compute
btnClear C&lear
btnExit E&xit
8/8/2015
6
Music and Videos 8. Set the Form's Accept and Escape
Set the Form's AcceptButton to btnCompute. The Enter key will cause the Event Handler for btnCompute to be executed.
Set the Form's CancelButton to btnClear. The Esc key will cause the Event Handler for btnClear to be executed.
Music and Videos 9. Change the Background Color
Change the background color so that the GroupBox, Textboxes and Buttons stand out. Pick colors that are pleasing to the eye. Change the background color of the form first. You will then need to change the background color of the lblTitle and the GroupBox.
8/8/2015
7
Music and Videos 10. Enter the Title Block and Constants
Double-click the Compute button and add the following code. Note that the constants are defined outside of any subroutine.
Music and Videos 11. Code for the Compute Button
Part
-1 o
f C
om
pu
te B
utt
on
Co
de
8/8/2015
8
Music and Videos 12. Code for the Compute Button
Part
-2 o
f C
om
pu
te B
utt
on
Co
de
Music and Videos 13. Code for Clear and Exit Buttons
8/8/2015
9
Music and Videos 14. Test the Program
Music = 5 Videos = 10
The program works correctly
If either textbox is empty or has non-numeric data, the program crashes because the CInt routine has a data type conversion error when trying to convert the text from the TextBox into an Integer value.
The program crashes
15. Exception Handling (Try/Catch)
Put all of the Input/Process/Output code inside a Try/Catch block. Any runtime errors detected by VB while in the Try block will automatically move execution of the code to the Catch block instead of crashing the program. If the CInt is not able to convert the text in txtMusic or txtVideo to an integer, the rest of the code in the Try block is skipped and the code in the Catch block is executed instead. A Try block is also a good idea to use when doing math calculations where there is a possibility of dividing by zero. These errors can also be handled by the Catch block.
8/8/2015
10
16. Test and Fix the Program Again
The program no longer crashes if a TextBox is empty or has non-numeric data, but it would be nice if an empty TextBox could be treated as a zero, meaning no music or videos were sold. The specification for the project also indicates that negative input values are to be rejected.
The .Trim on txtMusic.Text.Trim deletes leading and trailing spaces so that MusicSold will still be a zero even if spaces are entered into the TextBox and nothing else.
17. Test Everything Again
Make sure that the Alt-C Alt-L Alt-X each activate the proper buttons.
Make sure that the [Enter] and [Esc] keys activate their proper buttons.
Test the program with all positive numbers, empty TextBoxes, negative values, text input instead of numeric (try entering "five" instead of the number "5".