In this exercise, we will develop an application for a small CD publishing
company. The owner wants the prices to be calculated so a customer would get a
discount if he orders more. To attract customers and encourage them to order
more, she has decided to fix the prices so that the customer would pay:
- $20/CD if she orders less than 20 units
- $15/CD if she orders less than 50 units
- $12/CD if she orders less than 100 units
- $8/CD if she orders less than 500 units
- $5/CD if she orders more than 500 units
|
- Start Microsoft Visual Basic and create a Standard EXE application
- Save the application in a new folder named CD Publisher
- Save the form as frmMain and save the project as CDPublisher
- To use the needed spin button, on the main menu, click Project ->
Components...
- In the Components dialog box, click the Microsoft Windows Common
Controls 6.0 (SP4) check box
- Click OK
- Design it as follows:
|
Control |
Properties |
Form |
Name: frmMain
Border Style: 3 - FixedDialog
Caption: CD Publisher |
Label |
Caption: Number of CDs: |
TextBox |
Name: txtQuantity
Alignment: 1 - Right Justify |
UpDown |
Name: updQuantity
Alignment: 1
Buddy Control: txtQuantity
Max: 10000 |
Label |
Caption: Unit Price: |
TextBox |
Name: txtUnitPrice
Alignment: 1 - Right Justify |
Label |
Caption: Total Price: |
TextBox |
Name: txtTotalPrice
Alignment: 1 - Right Justify |
CommandButton |
Name: cmdClose
Caption: Close |
|
- Double-click the Close button and implement its Click event as
follows:
Private Sub cmdClose_Click()
End
End Sub
|
- Double-click the UpDown control and implement its Change event as follows:
Private Sub updQuantity_Change()
Dim Quantity As Integer
Dim UnitPrice, TotalPrice As Currency
Quantity = CInt(txtQuantity.Text)
If Quantity < 20 Then
UnitPrice = 20
ElseIf Quantity < 50 Then
UnitPrice = 15
ElseIf Quantity < 100 Then
UnitPrice = 12
ElseIf Quantity < 500 Then
UnitPrice = 8
Else
UnitPrice = 5
End If
TotalPrice = Quantity * UnitPrice
txtUnitPrice.Text = CStr(UnitPrice)
txtTotalPrice.Text = CStr(TotalPrice)
End Sub
|
- To allow the user to enter a quantity value, access the LostFocus
event of the txtQuantity text box and implement it as follows:
Private Sub txtQuantity_LostFocus()
updQuantity_Change
End Sub
|
- Test the application
|
|
|