Does anyone know how to accept percent and decimals in a text box? Also does anyone know how to limit the times a user can input data?
Here is a little background about the program i am working on:
I need to create an application that calculates and displays payroll for 4 employees. It accepts the following data from the user.
1) Num of hours worked.
2.) Hourly Rate.
3)Precent withheld for Fica.
4)Precent for federal.
5.Percent for State.
It then must NOT allow the user to enter anymore data after the 4 employees have been entered!
My problem is that I dont know how to allow the user to enter a percent in a textbox or decimal, if they enter either of those the program won't calculate it. But if i enter a whole number it works fine! Also I dont know how to limit the user from entering more then 4 names. I thought about using a Do While loop by but didnt know where to place it.
Here is my code
Here is a little background about the program i am working on:
I need to create an application that calculates and displays payroll for 4 employees. It accepts the following data from the user.
1) Num of hours worked.
2.) Hourly Rate.
3)Precent withheld for Fica.
4)Precent for federal.
5.Percent for State.
It then must NOT allow the user to enter anymore data after the 4 employees have been entered!
My problem is that I dont know how to allow the user to enter a percent in a textbox or decimal, if they enter either of those the program won't calculate it. But if i enter a whole number it works fine! Also I dont know how to limit the user from entering more then 4 names. I thought about using a Do While loop by but didnt know where to place it.
Here is my code
Code:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtState.Text = " "
txtFederal.Text = " "
txtFica.Text = " "
txtHours.Text = " "
txtRate.Text = " "
txtName.Text = " "
lstOutput.Items.Clear()
txtName.Focus()
End Sub
Private Sub txtHours_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtHours.Validating
'Vaildates that a number has been entered into the text box
If Not IsNumeric(txtHours.Text) Then
MessageBox.Show("Hours worked must be a number.", "Invaild Input", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
'Select the existing text in the text box.
txtHours.SelectionStart = 0
txtHours.SelectionLength = txtHours.Text.Length
'Set the e.Cacel to true so the focus will stay in the control
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
Private Sub txtRate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRate.Validating
'Vaildates that a number has been entered into the text box
If Not IsNumeric(txtRate.Text) Then
MessageBox.Show("Pay Rate must be a number.", "Invaild Input", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
'Select the existing text in the text box.
txtRate.SelectionStart = 0
txtRate.SelectionLength = txtRate.Text.Length
'Set the e.Cacel to true so the focus will stay in the control
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
Private Sub txtState_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtState.Validating
'Vaildates that a number has been entered into the text box
If Not IsNumeric(txtState.Text) Then
MessageBox.Show("State taxes must be a number.", "Invaild Input", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
'Select the existing text in the text box.
txtState.SelectionStart = 0
txtState.SelectionLength = txtState.Text.Length
'Set the e.Cacel to true so the focus will stay in the control
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
Private Sub txtFederal_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFederal.Validating
'Vaildates that a number has been entered into the text box
If Not IsNumeric(txtFederal.Text) Then
MessageBox.Show("Federal taxes must be a number.", "Invaild Input", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
'Select the existing text in the text box.
txtFederal.SelectionStart = 0
txtFederal.SelectionLength = txtFederal.Text.Length
'Set the e.Cacel to true so the focus will stay in the control
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
Private Sub txtFica_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtFica.Validating
'Vaildates that a number has been entered into the text box
If Not IsNumeric(txtFica.Text) Then
MessageBox.Show("F.I.C.A. withholdings must be a number.", "Invaild Input", MessageBoxButtons.OK, _
MessageBoxIcon.Error)
'Select the existing text in the text box.
txtFica.SelectionStart = 0
txtFica.SelectionLength = txtFica.Text.Length
'Set the e.Cacel to true so the focus will stay in the control
e.Cancel = True
Else
e.Cancel = False
End If
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'Declares local variables for btnCalculate_Click Method
Dim empName As String
Dim hrRate, hrsWorked As Single
Dim stateTax, fedTax As Single
Dim totalPay, grossPay, ficaTax As Single
'Gets data,compute payroll,display results
Call InputData(empName, hrRate, hrsWorked, fedTax, ficaTax, stateTax)
grossPay = Gross_Pay(hrRate, hrsWorked)
fedTax = Fed_Tax(grossPay, fedTax)
ficaTax = Fica_Tax(grossPay, ficaTax)
stateTax = State_Tax(grossPay, stateTax)
totalPay = CSng(Total_Pay(grossPay, ficaTax, fedTax, stateTax))
Call ShowPayroll(empName, grossPay, totalPay, ficaTax, fedTax, stateTax)
End Sub
Sub InputData(ByRef empName As String, ByRef hrRate As Single, ByRef hrsWorked As Single, ByRef fedTax As Single, ByRef ficaTax As Single, ByRef stateTax As Single)
' Get payroll data for employee
empName = txtName.Text
hrRate = CInt(txtRate.Text)
hrsWorked = CInt(txtHours.Text)
fedTax = CSng(txtFederal.Text)
ficaTax = CSng(txtFica.Text)
stateTax = CSng(txtState.Text)
End Sub
Sub ShowPayroll(ByRef empName As String, ByRef pay As Single, ByRef totalPay As Single, ByRef ficaTax As Single, ByRef fedTax As Single, ByVal stateTax As Single)
' Payroll output for listbox
lstOutput.Items.Add("Payroll results for " + (empName))
lstOutput.Items.Add(" Gross pay this period:" + FormatCurrency(pay))
lstOutput.Items.Add(" F.I.C.A. tax withheld:" + FormatCurrency(ficaTax))
lstOutput.Items.Add(" Federal Income tax withheld:" + FormatCurrency(fedTax))
lstOutput.Items.Add(" State Income tax withheld:" + FormatCurrency(stateTax))
lstOutput.Items.Add(" Net pay:" + FormatCurrency(totalPay))
End Sub
Function Gross_Pay(ByRef hrWage As Single, ByRef hrsWorked As Single) As Single
' Compute weekly pay before taxes
If hrsWorked <= 40 Then
Gross_Pay = hrsWorked * hrWage
Else
Gross_Pay = CSng(40 * hrWage + (hrsWorked - 40) * 1.5 * hrWage)
End If
End Function
Function Total_Pay(ByRef grossPay As Single, ByRef ficaTax As Single, ByRef fedTax As Single, ByRef stateTax As Single) As String
' Compute amount of net pay
Total_Pay = CStr(grossPay - ficaTax - fedTax - stateTax)
End Function
Function Fed_Tax(ByRef grossPay As Single, ByRef fedTax As Single) As Single
'Compute amount to be taken for Federal Tax
Fed_Tax = grossPay * fedTax
End Function
Function Fica_Tax(ByRef grossPay As Single, ByRef ficaTax As Single) As Single
''Compute amount to be taken for FICA Tax
Fica_Tax = grossPay * ficaTax
End Function
Function State_Tax(ByRef grossPay As Single, ByRef stateTax As Single) As Single
'Compute amount to be taken for State Tax
State_Tax = grossPay * stateTax
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class