-
Nov 13th, 2013, 04:48 PM
#1
Thread Starter
New Member
I need help with creating a payroll in visual basic
create an application that displays payroll information. The application should allow the user to enter the following data for four employees:
number of hours worked
hourly pay rate
percentage to be withheld for state income tax
percentage to be withheld for federal income tax
percentage to be withheld fo FICA
The application should calculate and display the following data for each employee in a list box:
gross pay (the number of hours worked multiplied by hourly pay rate)
state income tax withholdings (gross pay multiplied by state income tax percentage)
federal income tax withholdings (gross pay multiplied by federal income tax percentage)
FICA withholdings (gross pay multiplied by FICA percentage)
net pay (the gross pay minus state income tax, federal income tax, and FICA)
When the calculations are performed, be sure to check for the following error:
if an employee's state income tax plus federal tax plus FICA is greater than the employee's gross pay, display an error message stating that the withholdings are too great.
Be sure to add appropriate ToolTips for each control on the form.
-
Nov 13th, 2013, 04:54 PM
#2
Frenzied Member
Re: I need help with creating a payroll in visual basic
So you have your requirements done, what code have you written so far?
-
Nov 13th, 2013, 05:00 PM
#3
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
This is all I have done so far
-
Nov 13th, 2013, 05:08 PM
#4
Re: I need help with creating a payroll in visual basic
A picture of the UI != code
-
Nov 13th, 2013, 05:20 PM
#5
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
-
Nov 13th, 2013, 05:20 PM
#6
Re: I need help with creating a payroll in visual basic
But it is enough to know that it won't quite be sufficient.
That form would allow you to enter information for one employee, or to show information for one employee, but how will you use that to deal with four employees? How were you figuring you would move from one employee to the next? One of the things you'd have to do for that navigation is to be able to store the information for each employee. Technically, that's not so difficult, but since this is an assignment, you are probably limited to the things you have already learned. So, have you learned classes yet? If not, how about arrays or better yet, Lists?
My usual boring signature: Nothing
-
Nov 13th, 2013, 05:22 PM
#7
Re: I need help with creating a payroll in visual basic
Posting pictures isn't a good way to go here. Post the code directly, properly wrapped in [Code] [/CODE] tags.
My usual boring signature: Nothing
-
Nov 13th, 2013, 05:23 PM
#8
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
'input variable declaration.
Dim hoursWorked As Decimal
Dim payrate As Decimal
Dim stateincometax As Decimal
Dim federalincometax As Decimal
Dim FICA As Decimal
'Getting input.
strname = InputBox("Enter Name:", "Enter Input")
'Calculate grosspay, stateIncome, federalIncome, fica and netpay.
dblgrosspay = inthour * inthourRate
dblstateIncome = dblgrosspay * dblstate
dblfederalIncome = dblgrosspay * dblfederal
dblfica = dblgrosspay * fciaIn
-
Nov 13th, 2013, 05:32 PM
#9
Re: I need help with creating a payroll in visual basic
Ok, that's something...but what? For one thing, you should wrap code in the tags that I mentioned.
Some people, and therefore some classes, still like warting variables, which is what you are doing with the prepended int and dbl. There isn't much point in that anymore, though I still do a small amount of it to make things group together in intellisense, and to tell me the scope of some variables. However, in this case it leads to a few questions:
1) If intHourRate really is an Integer, that's a problem. Few people are paid an integer amount as an hourly rate. More typically, they are paid a Decimal amount. If you are taking a Decimal value and stuffing it into an integer, you will lose the decimal portion of the number.
2)You have a few locally declared variables, such as Dim FICA As Decimal, but then you use fciaIn. Where did fciaIn come from? What is the point of declaring the FICA variable if you never put anything into it and never use it anywhere? The same could be said of dblState vs. stateincometax, and the rest.
3) When working with money, Decimal is the right type, but the warting suggests that you are turning much of this into type Double. It should all be Decimal and remain Decimal, otherwise you will get odd rounding issues in some cases.
4) Where are you storing the information for each person?
My usual boring signature: Nothing
-
Nov 13th, 2013, 05:39 PM
#10
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
could you write this in code? so I can understand it better
-
Nov 13th, 2013, 06:18 PM
#11
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
can someone please help me out
-
Nov 13th, 2013, 06:40 PM
#12
Re: I need help with creating a payroll in visual basic
Write what in code? I'm not sure I said anything that translates into much code. You have some variables that you don't appear to be declaring. Either they are declared at form scope and not in the method, or they are not declared at all. The latter would be a problem, but there isn't much writing to do for that.
My usual boring signature: Nothing
-
Nov 13th, 2013, 06:50 PM
#13
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
This is all I have written what should I do next is what am asking
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'input variable declaration.
Dim hoursWorked As Decimal
Dim payrate As Decimal
Dim stateincometax As Decimal
Dim federalincometax As Decimal
Dim FICA, fciaIn As Decimal
-
Nov 13th, 2013, 10:26 PM
#14
Re: I need help with creating a payroll in visual basic
Those look like variables to hold the inputs, so you would convert the inputs into the variables using Decimal.TryParse:
Code:
If Decimal.TryParse(someTextbox.Text, hoursWorked) Then
'It was a decimal and was parsed into the hoursWorked variable.
Else
'It was not a decimal, so put up a messagebox and exit the sub.
End If
Of course, I don't know what the names of the controls are, so I used a dummy value for the textbox name, but you know what the names are, so you can correct that.
My usual boring signature: Nothing
-
Nov 14th, 2013, 10:25 AM
#15
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
here is my code
Public Class Form1
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'input variable declaration.
Dim Name As Decimal
Dim hoursWorked As Decimal
Dim payrate As Decimal
Dim stateincometax As Decimal
Dim federalincometax As Decimal
Dim FICA, fciaIn As Decimal
If Decimal.TryParse(txtname.Text, Name) Then
'it was a decimal and was parsed into the name variable.
Else
'It was not a decimal, so put a messagebox and exit the sub.
End If
If Decimal.TryParse(txthours.Text, hoursWorked) Then
'It was a decimal and was parsed into the hoursWorked variable.
Else
'It was not a decimal, so put up a messagebox and exit the sub.
End If
If Decimal.TryParse(txtrate.Text, payrate) Then
'It was a decimal and was parsed into the payrate variable.
Else
' It was not a decimal, so put up a messagebox and exit the sub.
End If
If Decimal.TryParse(txtstate.Text, stateincometax) Then
'It was a decimal and was parsed into the stateincometax variable.
Else
'It was not a decimal, so put a messagebox and exit the sub.
End If
If Decimal.TryParse(txtfederal.Text, federalincometax) Then
'It was a decimal and was parsed into the federalincometax variable.
Else
'It was not a decimal, so put a messagebox exit the sub.
End If
If Decimal.TryParse(txtFICA.Text, FICA) Then
'It was a decimal and was parsed into the FICA variable.
Else
'It was not a decimal, so put a messagebox exit the sub.
End If
If Decimal.TryParse(txtFICA.Text, fciaIn) Then
'It was a decimal and was parsed into the fciaIn variable.
Else
'It was not a decimal, so put a messagebox exit the sub.
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtstate.Text = " "
txtfederal.Text = " "
txtFICA.Text = " "
txthours.Text = " "
txtrate.Text = " "
txtName.Text = " "
IstOutput.Items.Clear()
txtName.Focus()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
clear and exit is working but calculate isnt
-
Nov 14th, 2013, 10:34 AM
#16
Re: I need help with creating a payroll in visual basic
Hi t - baby, welcome to the forums.
I know shaggy mentioned earlier that you need to wrap your code in code tags: [code]'Hello World[/code] please do so. It properly formats your code, for example your btnClear and btnExit code wrapped in code tags look like this:
Code:
Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
txtstate.Text = " "
txtfederal.Text = " "
txtFICA.Text = " "
txthours.Text = " "
txtrate.Text = " "
txtname.Text = " "
IstOutput.Items.Clear()
txtname.Focus()
End Sub
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
As for the reason why your btnCalculate isn't working, it's because you haven't written any code for it. You just check if the user entered in a decimal, but then comment everything out.
Also, should name be declared as a decimal or should it be declared as a string?
-
Nov 14th, 2013, 10:38 AM
#17
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
it should be written as string, can you help me with the code for calculate??
-
Nov 14th, 2013, 10:41 AM
#18
Re: I need help with creating a payroll in visual basic
No because I'm not a math guy. Come up with the equations to calculate these four things:
- hourly pay rate
- percentage to be withheld for state income tax
- percentage to be withheld for federal income tax
- percentage to be withheld fo FICA
and I can help you convert those equations into code.
-
Nov 14th, 2013, 10:46 AM
#19
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
hourly pay rate <= 20
percentage for state income tax = 2%
percentage for federal income tax = 5%
percentage for FICA = 3%
is this okay??
-
Nov 14th, 2013, 11:01 AM
#20
Re: I need help with creating a payroll in visual basic
Well, that's not what I meant... Let's work this out. I would first change your code in the calculation. The first change is to the name:
Code:
Dim Name As String = txtname.Text
Dim hoursWorked As Decimal
Dim payrate As Decimal
Dim stateincometax As Decimal
Dim federalincometax As Decimal
Dim FICA, fciaIn As Decimal
The next change will be to use AndAlso when trying to convert the text to a decimal, and if any of them fail, let the user know:
Code:
Dim Name As String = txtname.Text
Dim hoursWorked As Decimal
Dim payrate As Decimal
Dim stateincometax As Decimal
Dim federalincometax As Decimal
Dim FICA, fciaIn As Decimal
If Decimal.TryParse(txthours.Text, hoursWorked) AndAlso _
Decimal.TryParse(txtrate.Text, payrate) AndAlso _
Decimal.TryParse(txtstate.Text, stateincometax) AndAlso _
Decimal.TryParse(txtfederal.Text, federalincometax) AndAlso _
Decimal.TryParse(txtFICA.Text, FICA) Then
Else
MessageBox.Show("One or more text fields has an invalid input.")
End If
Next, let's work on the equations:
- gross pay (the number of hours worked multiplied by hourly pay rate)
- state income tax withholdings (gross pay multiplied by state income tax percentage)
- federal income tax withholdings (gross pay multiplied by federal income tax percentage)
- FICA withholdings (gross pay multiplied by FICA percentage)
- net pay (the gross pay minus state income tax, federal income tax, and FICA)
Those are simple equations:
Code:
Dim Name As String = txtname.Text
Dim hoursWorked As Decimal
Dim payrate As Decimal
Dim stateincometax As Decimal
Dim federalincometax As Decimal
Dim FICA, fciaIn As Decimal
If Decimal.TryParse(txthours.Text, hoursWorked) AndAlso _
Decimal.TryParse(txtrate.Text, payrate) AndAlso _
Decimal.TryParse(txtstate.Text, stateincometax) AndAlso _
Decimal.TryParse(txtfederal.Text, federalincometax) AndAlso _
Decimal.TryParse(txtFICA.Text, FICA) Then
Dim grosspay As Decimal
Dim statewithholdings As Decimal
Dim federalwithholdings As Decimal
Dim ficawithholdings As Decimal
Dim netpay As Decimal
grosspay = hoursWorked * payrate
statewithholdings = grosspay * stateincometax
federalwithholdings = grosspay * federalincometax
ficawithholdings = grosspay * FICA
netpay = grosspay - statewithholdings - federalwithholdings - ficawithholdings
Else
MessageBox.Show("One or more text fields has an invalid input.")
End If
So in the end, I wound up not even using the fciain variable nor the name variable.
Last edited by dday9; Nov 14th, 2013 at 11:09 AM.
Reason: When I added font color, it messed up the code tags. So I fixed it.
-
Nov 14th, 2013, 02:45 PM
#21
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
thanks, I did this but still the calculate button wont work..
-
Nov 14th, 2013, 02:46 PM
#22
Re: I need help with creating a payroll in visual basic
Ok, so what have you tried to fix it?
-
Nov 14th, 2013, 02:51 PM
#23
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
the 1stOutput is meant to show the calculations but its not showing anything.. how can i create a code for the 1stOutPut. This is the listbox where all calculations is meant to be happening
Last edited by Tee Baby; Nov 14th, 2013 at 02:54 PM.
-
Nov 14th, 2013, 03:03 PM
#24
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
I just created this and I added it too it.
lblOutput.Items.Add("Employee Name" + "Gross Pay State Income Tax" + "Federal Income Tax" + "FICA Net Pay"
-
Nov 14th, 2013, 03:16 PM
#25
Re: I need help with creating a payroll in visual basic
I think at this point, it's painfully obvious that you don't have a good understanding of the basics. Before I'm going to help you out any more, I suggest that you look at: http://homeandlearn.co.uk/NET/vbNet.html
-
Nov 14th, 2013, 03:26 PM
#26
Thread Starter
New Member
Re: I need help with creating a payroll in visual basic
alright thanks for your help and have a nice day
-
Nov 14th, 2013, 04:13 PM
#27
Addicted Member
Re: I need help with creating a payroll in visual basic
This is how you must perform calculations.
Also I provided a solution how to put them in listbox.
vb.net Code:
[Private Sub btnCalc(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click Dim Message As String = "The withholdings are too great. " Dim hoursWorked As Decimal Dim federalTax As Decimal Dim hoursPayRate As Decimal Dim stateTax As Decimal Dim ficaTax As Decimal Dim federalTaxAmount As Decimal Dim stateTaxAmount As Decimal Dim ficaTaxAmount As Decimal Dim grossPay As Decimal Dim totalTax As Decimal Dim netPay As Decimal Decimal.TryParse(txtHoursWorked.Text, hoursWorked) Decimal.TryParse(txtFederalTax.Text, federalTax) Decimal.TryParse(txtHoursRate.Text, hoursPayRate) Decimal.TryParse(txtStateTax.Text, stateTax) Decimal.TryParse(txtFica.Text, ficaTax) federalTax = federalTax / 100 stateTax = stateTax / 100 ficaTax = ficaTax / 100 grossPay = hoursWorked * hoursPayRate federalTaxAmount = grossPay * federalTax stateTaxAmount = grossPay * stateTax ficaTaxAmount = grossPay * ficaTax totalTax = federalTaxAmount + stateTaxAmount + ficaTaxAmount If totalTax > grossPay Then MessageBox.Show(Message, "Payroll information", MessageBoxButtons.OK, MessageBoxIcon.Information) Exit Sub Else netPay = grossPay - totalTax lstOutput.Items.Add("Gross pay: " & grossPay.ToString("N2").PadLeft(20)) lstOutput.Items.Add("Federal income tax: " & federalTaxAmount.ToString("N2").PadLeft(15)) lstOutput.Items.Add("State income tax: " & stateTaxAmount.ToString("N2").PadLeft(15)) lstOutput.Items.Add("Fica withholdings tax: " & ficaTaxAmount.ToString("N2").PqdLeft(15)) lstOutput.Items.Add("Net pay: " & netPay.ToString("N2").PadLeft(15)) End If End Sub
Before, I couldn't wrap code until I saw a post of Jmcilhinney, thanks to him.
Last edited by Atenk; Nov 14th, 2013 at 05:13 PM.
Reason: Edit
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|