In this example, we are going to build an application to calculate the daily wage of temporary employees.
To compute the daily wage we need following inputs
- The number of Hours Employee worked.
- Hourly pay rate
The application computes the gross pay and displays it on the screen.
Wage Calculator Interface
To design the interface we go step by step.
Step 1: Open a new vb form and name it Daily Wage Calculator and save the project as wage_calculator. The following diagram shows the new form which we renamed.
You will get a new form as soon as you open the standard VB project windows. To change the name and caption of the form, go to property window located to the right-bottom of the project window.
In the property window, change the form name to Daily_Wage_Calc and caption to Daily Wage Calculator.
Step 2: Add Controls
Now its time to add more control. To complete the interface you need – two textboxes, 5 labels, and two command buttons.
The detailed settings for each control element are given below.
Textboxes
Name: txtHoursWorked Caption: ' ' Height: 500 Width: 2400
Name: txtHoulyRate Caption: ' ' Height: 500 Width: 2400
Labels
Name : lblTotEarnDisplay Caption: ' ' Height: 500 Width: 2400
Name: lblHoursWorked Caption: Hours Worked Height: 495 Width: 1995
Name: lblHourlyRate Caption: Hourly Rate Height: 495 Width: 1995
Name: lblTotEarning Caption: Total Earnings Height: 495 Width: 1995
Command Buttons
Name: cmdGross Caption: &Gross Pay Height: 700 Width: 2400
Name: cmdClose Caption: &Close Height: 700 Width: 2400
The interface will look like the following after adding all the controls.
To align or to adjust the spacing between the controls go to Format tab > select Align or select Horizontal or Vertical spacing options.
You can add color to background and foreground in the property window so that the application look better visually.
Coding the Daily Wage Calculator Application
The application design is ready but it does not do anything. You need to write code for the application to work.
Step 3: Understand the logic and write code for the wage calculator application. You can do this in the following stages,
- Get the inputs from textboxes
- Store them in variables
- Compute the gross pay
- Display the output
First, we declare three variables to get the input values and use them later. We will store them in the variables.
Dim hours As Integer Dim rate As Integer Dim gross As Integer
Code for textboxes
Now write the code for textboxes are follows.
Private Sub txtHoursWorked_Change() hours = Val(txtHoursWorked.Text) End Sub
Private Sub txtHourlyRate_Change() rate = Val(txtHourlyRate.Text) End Sub
Code for Command Buttons
The code for command buttons is given below. To compute gross pay, the value of hoursrategross$.
\begin{aligned}gross pay = hours \hspace{3px} worked \times hourly \hspace{3px} rate\end{aligned}
The code for command button gross pay. Note than the caption for lblTotEarningDisplay is used to display the output.
Private Sub cmdGross_Click() gross = hours * rate lblTotEarnDisplay.Caption = Str(gross) End Sub
The code for command button close is as follows.
Private Sub cmdClose_Click() Unload Daily_Wage_Calc End Sub
Test the Application
Now that we have coded the application, it is time to test the program.
The calculator is correctly calculating the daily wage of an employee given the inputs required.