Ever wondered how much you make at your work? Well you should already know but if not then we can create a calculator that calculates gross pay automatically. No need to multiply yourself, just click a button and the amount will show. This is a simple application, so do not over analyze things, just pay attention to what we are doing and if you have trouble just repeat the steps and in time you will learn.

Setting Up

If you have not done so already, open visual studio and open a windows form application. Click on File > New > Project. Expand Other Languages > Visual Basic and click on Windows Forms Application. Name the file Gross Pay Calculator and click OK.

Step One – Form Design

This application will use only two buttons, the calculate and close buttons, the rest of the form design will consist of labels and text boxes.

On the form we need to add four labels, so in the Toolbox panel, find the Label item and place it four times on the form. Positioning doesn’t matter at this moment because we have not named the labels yet. The labels automatically generate names for each label, so to help lessen the possible confusion I will list the labels with the generated names and beside it mark the changes made to it. The ‘(Name)’ and ‘Text’ values can be found in each label’s properties.

Old Name New Name New Text
Label1 lblHrsWorked Number of Hours Worked
Label2 lblHourlyPay Hourly Pay Rate
Label3 lblGrossPay Gross Pay Earned
Label4 lblGrossPayEarned (Delete text)

Next we need to add two text boxes for the hours worked and hourly pay rate. We use this by adding two text boxes from the Toolbox. Open the Toolbox and drag two TextBox items onto the form, one next to lblHrsWorked and change it’s ‘(Name)’ to txtHrsWorked, and the other next to lblHourlyPay with a ‘(Name)’ of txtPayRate.

Now that we have our labels and text boxes set, we need to add two buttons to calculate and close the application on the users request. Grab two Button items form the Toolbox panel and place them on the form. Name one button Calculate Gross Pay and change the text to btnCalculate. Name the other button to Close and change the text to btnClose.

Gross Pay Calc 1

This is one example of what the calculator can look like; the design is totally up to you

Step Two – Form1.vb

Form1.vb handles the events on the form. For example, we will have to click events for the buttons and Form1.vb will handle the code. Double click on Calculate Gross Pay to create an event handler. Using the visual basic language, we can now code what happens when this specific button is clicked. Naturally, given the name of the button this event calculate the gross pay for the user.

We will use what is called an ‘If’ statement that has the ability to handle multiple events and have different outcomes for each event. Its an easy but yet very useful tool in the programming world. Well basically in our If statement, we test if both inputs from the user are of numerical value with the ‘IsNumeric’ method and if so we multiply the hours worked by the hourly pay rate, but if not we output a message box that informs the user to enter numerical values.

Now go back to design view of the form and double click on the Close button to create an event handler for it. This code will be very short. All we have to do is type in code that closes the form on this event, we do that by typing Me.Close(). I wasn’t kidding when I said it was short.

Output

Run the application and try it out. In the screen shot below I entered 40 as the hours worked, 10 as the hourly pay rate and the gross pay was calculated to be 400.

Gross Pay Calc 2

Its a neat application to have. Its not the most complicated thing but it is useful and a stepping stone for those harder projects.

Download Source Files