Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

1

CS 416 – C# Programming

Practice Laboratory Exercise 2 – Gross and Net Pay Calculator

Objective: To create a program that demonstrates the function of MS Visual Studio objects and
Mathematical operations.

Instruction:
1. Start a new Visual C# 2010 Windows Application project.
2. Name your project as prjPracLabExer2. Save your file in your working folder. See
the following figure for example:

3. Change the Text Property of your form (Form1) to “Gross and Net Pay Calculator.” The
following steps demonstrate:

To change your form Text Property:


1. Click your form in Design View.
2. In Properties Window, find Text property and change Form1 to “Gross and
Net Pay Calculator”. See the following figure for example:
2
The caption of your form (Form1) will change to “Gross and Net Pay Calculator”.
See the following figure for example:

4. Rename your form to frmGNPCalculator.cs. The following steps will demonstrate:

To rename your form:


1. In Solution Explorer Window, Right-click in Form1.cs.
2. Select Rename from the pop-up menu. See the following figure for example:

Change Form1.cs to frmGNPCalculator.cs and press Enter. See the following


figure for example:

Note: Do not remove the “.cs” extension to your form to let your Visual C#
project recognize your file.
3

5. Add three Group Box controls in your form. From the Toolbox Window, select
GroupBox from Containers group of items. See the following figure for example and
the following steps demonstrate:

To add a Group Box control in your form:


1. Select GroupBox control from Container group of items.
2. Double-click GroupBox control to be added in your form. See the
following figure for example:

3. Move and resize the first Group Box control to your desired location. See
the following figure for example:
4

4. Change the Text Property of your Group Box control to “Gross Income”. See
the following figure for example:

5. Add two more Group Box control below of Gross Income and change the Text
Properties to “Deductions” and “Net Income”. Resize your form if necessary
for your group box controls. See the following figure for example:
5

6. Add a two Button controls in your form (below of Net Income group box control),
the Compute and Exit buttons. Change the Name property of Compute button to
“cmdCompute” and Exit to “cmdExit”. See the following figure for example:
6

7. Add a three Text Box controls in your Gross Income group box with an associated Label,
the “Hours Worked”, “Pay Rate”, and “Gross Income”. See the following figure for
example:

8. Change the Name Property of your Text Box controls to “txtHoursWorked”,


“txtPayRate”, and “txtGrossIncome” with its associated labels.
9. Add another five Text Box controls in Deductions group box with associated Labels, the
“SSS”, “Pag-Ibig”, “Philhealth”, “Others”, and “Total Deductions”. You can resize your
form to accommodate the number of newly added objects.
7

void cmdCompute_Click(object sender, EventArgs e) {

10. Change the Name Property of your new Text Box controls in Deductions group box to
“txtSSS”, “txtPagIbig”, “txtPhilhealth”, “txtOthers”, and “txtTotalDeductions”. You can
refer to its associated labels for the possible names.
11. Add a Text Box control in your Net Income group box with associated label, the “Net
Income”. Resize your group box control if needed. See the following figure for
example:
12. Change the Name Property of your new Text Box control in Net Income group box to
“txtNetIncome.”
13. Double-click your Compute button and type the following codes between the private
And }.

//variable for mathematical operation


double HoursWorked, PayRate, GrossIncome;
double SSS, Pagibig, Philhealth, Others, TotalDeductions;
double NetIncome;

//passing of values from object to variable


HoursWorked = Convert.ToDouble(txtHoursWorked.Text);
PayRate = Convert.ToDouble(txtPayRate.Text);

//mathematical operation for Gross Income


GrossIncome = HoursWorked * PayRate;

//passing of value from Gross Income to txtGrossIncome


8

txtGrossIncome.Text = GrossIncome.ToString();

//passing of value from object to variable


SSS = Convert.ToDouble(txtSSS.Text);
Pagibig = Convert.ToDouble(txtPagibig.Text);
Philhealth = Convert.ToDouble(txtPhilhealth.Text);
Others = Convert.ToDouble(txtOthers.Text);

//mathematical operation for Total Deduction


TotalDeductions = SSS + Pagibig + Philhealth + Others;

//passing of value from variable to txtTotalDeduction


txtTotalDeductions.Text = TotalDeductions.ToString();

//mathematical operation for Net Income


NetIncome = GrossIncome - TotalDeductions;

//passing of value from variable to txtNetIncome


txtNetIncome.Text = NetIncome.ToString();
9

See the following figure below for your example code listed in Visual C# code editor.

14. Type Close() in your cmdExit button control procedure. See the following code
listing below for example:
10

15. Run and test your program (the Gross and Net Pay Calculator). Enter the
following values from the given interface below:

Note: Gross Income, Total Deductions, and Net Income should be blank for there is
already an associated code to assign its value as you click the Compute button.
Please study and understand the given codes in your Compute button.
11

16. Click the Compute button to let your program compute for the “Gross Income,” “Total
Deductions,” and the “Net Income.” See the following figure for example:

17. Click the Exit button to close your form (running program).
18. Save your project in your working folder.

Note: Prepare for your Laboratory Exercise 1. Please always read and understand the given
instructions. Read and learn from error messages as you move to the next level of Visual C#
programming.

You might also like