Beginning Visual Basic - Project 5
Cash Register Program
The 5th Project
Here’s your chance to use what you have learned. This program will simulate a simple Cash Register. On a blank form you will place 4 Textboxes: One each for Cost, Quantity, Tax Rate, and Total Price. Each Textbox will be labeled with a Label control. You used Labels in the last project (if you did the enhancements). Labels are probably the simplest Controls of all. Labels come with Event Procedures like other controls, but it’s rare that you’ll ever use them. All you’re going to use Labels for in this program is to label your Textboxes. Besides the 4 Textboxes, and 4 Labels, you’re also going to add 3 Command Buttons. One Button each for Clear (to clear the contents of the Textboxes), Calculate (to calculate the Total Price from the Cost, Quantity, and Tax Rate), and Exit. Oh, and don’t forget to add one more Label control as a title. Below is an example of the program, but you do not need to copy this layout. Besides containing the essential Controls shown, you can make it look any way you like:
Begin with a blank form (Choose New Project under the File menu, make it a Standard project) and press the F4 key to view the form’s properties if the Properties Window is not already displayed.
- Change the Caption property of the form to Project 5
- Change the default Name (form1) to frmProj5
Open the Properties dialog by selecting Project1 Properties under the Project drop-down menu. Make sure the General tab on the Properties dialog is selected.
In the Startup Object combobox make sure frmProj5 is selected (it should be by default). In the Project Name textbox type Project5. In the Project Description textbox type the following: Simple Cash Register program. Leave all other settings at their defaults and click the OK button.
Pull down the File menu and choose Save Project. Save the form as: frmProj5.frm. Save the project as project5.vbp.
Following are 3 sections with 3 levels of help designated: Hacker, User, and Nubie.
The Hacker level is the hardest, with the least amount of instruction—you’re pretty much on your own. For those of you who like to figure it out for themselves, try the Hacker help section first.
The User help section is for those who like to figure things out for themselves, but need a little extra information.
The Nubie (which is computerese for beginner) help section is more instructive than Hacker or User and might give you that one hint that gets you moving when you’re stuck.
Please try to figure out as much as you can on your own. Begin with the Hacker help page and only refer to the User and Nubie help pages when you get stuck. When you finish with the basic project, don’t forget to try and complete the Recommended Enhancements on the last page.
Hacker Help Section
I have only a few suggestions for you to keep me happy:
- Follow the proper naming conventions for your Form, Controls, and Variables that you were using in the first 4 projects.
- To make the Total Price value appear as currency, investigate the Format function in Visual Basic Help.
- Don’t try and make this program more complicated than it needs to be.
User Help Section
First read the instructions on the Hacker help page above.
Here are the commands to calculate the Total Price as they should appear in the cmdCalc_Click() event procedure:
Dim dTotal As Double
‘Calculate the total amount
dTotal = Val(txtCost.Text) * Val(txtQuantity.Text)
‘Calculate the Tax and add it to the total
dTotal = dTotal + (dTotal * Val(txtTaxRate.Text))
‘Format the total as currency and copy it to the txtTotal textbox
txtTotal.Text = Format(dTotal, "Currency")Get Help on the Val and Format functions to understand what they do.
Here’s a hint for the Clear button. To clear the text in a Textbox set the Text property of the Textbox to an empty string. You can set the value of a string to null by making the string equal to "" (that’s double-quotes followed immediately by double-quotes with no space in-between) like so:
txtCost.Text = ""
This command erases any previous value in the Text property of the txtCost textbox control.
The font I used in the example in both the Labels and the Buttons was:
Font - Arial
Size - 14 point
Attribute - Bold
You can set these with the Font property of the Labels and Buttons.
Nubie Help Page
Use the illustration at the beginning of this assignment as a guide and place the 4 Textboxes on the form as shown. Place 4 Labels one to the left of each Textbox. Place another Label control near the top center of the form for the title. Now add 3 Command Buttons where shown. Use the following Properties/Procedures Table to set the designated property values:
Properties
Object | Property | Setting |
Form | Name | frmProj5 |
Caption | Project 5 | |
Command Button | Name | cmdClear |
Caption | Cl&ear | |
Font | Arial (14 pt. Bold) | |
Command Button | Name | cmdCalc |
Caption | &Calculate | |
Font | Arial (14 pt. Bold) | |
Command Button | Name | cmdExit |
Caption | E&xit | |
Font | Arial (14 pt. Bold) | |
Text Box | Name | txtCost |
Text | blank | |
Text Box | Name | txtQuantity |
Text | blank | |
Text Box | Name | txtTaxRate |
Text | blank | |
Text Box | Name | txtTotal |
Text | blank | |
Locked | True | |
Label | Name | lblCost |
Caption | Cost: | |
Font | Arial (14 pt. Bold) | |
Label | Name | lblQuantity |
Caption | Quantity: | |
Font | Arial (14 pt. Bold) | |
Label | Name | lblTaxRate |
Caption | Tax Rate: | |
Font | Arial (14 pt. Bold) | |
Label | Name | lblTotal |
Caption | Total Amount: | |
Font | Arial (14 pt. Bold) | |
Label | Name | lblTitle |
Caption | My Cash Register | |
Font | Arial (14 pt. Bold) |
Event Procedures
Object | Procedure | Code |
cmdExit | cmdExit_Click() | End |
cmdClear | cmdClear_Click() | txtCost.Text = "" txtQuantity.Text = "" txtTaxRate.Text = "" txtTotal.Text = "" |
cmdCalc | cmdCalc_Click() | Dim dTotal As Double dTotal = Val(txtCost.Text) * Val(txtQuantity.Text) dTotal = dTotal + (dTotal * Val(txtTaxRate.Text)) txtTotal.Text = Format(dTotal, "Currency") |
The cmdClear_Click event procedure sets all the values in the Text properties of the 4 Textboxes to empty strings (Two double quotes, one after the other with no space in-between, means empty string or literally nothing. That’s how you make a string blank).
You dimension a variable as a Double named dTotal in the cmdCalc_Click event procedure. Double precision numbers can contain real numbers with decimal values (which is what you need when dealing with currency). The Val function turns a string into a number. The Format function, puts a number into one of several formats. Get Visual Basic help on the Val and Format functions to find out how they work (also get help on CInt, CDbl, and CCur).
Recommended Enhancements
Make your program allow the user to move from one textbox to the next by pressing the Enter key. Hint: Here’s some code that goes in the txtCost_KeyPress event procedure that will set the focus on the txtQuantity textbox when the user presses the Enter key:
Private Sub txtCost_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
txtQuantity.SetFocus
KeyAscii = 0 ‘Stop the beep by setting KeyAscii to zero
End IfEnd Sub
KeyAscii is an integer that is passed to the KeyPress event procedure by the operating system. It is equal to the ASCII value of the key that was pressed. vbKeyReturn is a visual basic constant that represents the ASCII value of the Enter key. Visual Basic constants like this are available for all the keys on the keyboard (i.e. vbKeyA for capital ‘A’, vbKeyEnd for the End key, etc.).
What will adding this code to the txtCost_LostFocus event procedure do? Try it and see:
txtCost.Text = Format(txtCost.Text, "Currency")
Note: The Val conversion function can’t convert a string that’s in Currency format ($#,##.##) to a number. Use the CCur conversion function for that.
Add a another Textbox (txtGrandTotal) that displays a running total (Grand Total) of all the values that get calculated in the txtTotal textbox. Add a separate Clear button for the Grand Total textbox.