If Statement in VB .NEt
5 posts
• Page 1 of 1
If Statement in VB .NEt
I need help. Here is my problem I need an if statement that would stop the user from entering more data into the textboxes after they enter the data 4 times. I dont know how to do it as of now.
Here is my code.
- Code: Select all
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
Thank you for your help!
- Guest
Posts of this kind are not going to get any attention. First off, I would suggest registering with the site. Second, the code you have posted is way too long. I am not a VisualBasic programmer but even if I was I would not sort through what you have posted. What methods have you tried to implement this loop? What have your errors been? Lastly, when posting code that is longer than, say 20 lines, try to use a service like www.nomorepasting.com and then link to your code.
If you want a post like this to get attention, please clarify and simplify the post.
Thanks.
-Weaver
Public Keys
The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change.
-- FORTRAN manual for Xerox Computers
The primary purpose of the DATA statement is to give names to constants; instead of referring to pi as 3.141592653589793 at every appearance, the variable PI can be given that value with a DATA statement and used instead of the longer form of the constant. This also simplifies modifying the program, should the value of pi change.
-- FORTRAN manual for Xerox Computers
- Weaver
- PROfessional Member
- Posts: 1967
- Joined: Wed Jun 19, 2002 12:05 am
- Location: /home/weaver/
i don't really get the question.. like what you mean after entering it 4 times? after doing the validate function 4 times or what? well either way whereever the 4 times is into play just make a global variable and then incriment it in the function and once hit reaches 4 then disable the text fields
not hard.
If i knew exactly what triggered it i could do the if statement but i don't know what you want to trigger it with
not hard.
If i knew exactly what triggered it i could do the if statement but i don't know what you want to trigger it with
<a href="http://www.secretsofwar.net/index.asp?referer=injunfarian" target=_blank>Good Webbased Game</a>
<a href="http://selectaz.com" target=_blank><img src="http://selectaz.com/popup/stats.php" border=0></a>
<a href="http://selectaz.com" target=_blank><img src="http://selectaz.com/popup/stats.php" border=0></a>
- Injunfarian
- Posts: 63
- Joined: Sat Jan 18, 2003 8:15 pm
i'd do something along these lines ...
- Code: Select all
Private clickValue As Integer = 0 '/// place this above your subs.
'/// then...
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
'/// reset entry options for TextBox1
Dim defaultLength As Integer = 32767 '/// standard maximum length of a textbox.
TextBox1.MaxLength = defaultLength
clickValue = 0
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If clickValue < 4 AndAlso IsNumeric(TextBox1.Text) Then
'/// carry out your additional code here...
clickValue += 1
ElseIf clickValue < 4 AndAlso Not IsNumeric(TextBox1.Text) Then
MessageBox.Show("Only numbers allowed , 3 chances left!", Me.Name, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
TextBox1.Clear()
clickValue += 1
Else
TextBox1.MaxLength = 0
TextBox1.Clear()
clickValue += 1
End If
End Sub
- dynamic_scripter
- Posts: 5
- Joined: Wed Mar 03, 2004 10:59 pm
- Location: UK
Next time, simply put the subs header, their names should be intuitive, so we get your problem more easily... Seems like you are about to paste the entire solution!
The objective is not to flame, in truth, just be more objective in terms of algorithm. Your form class should include a public var that gets increased/decreased when a form or button get a click.
The objective is not to flame, in truth, just be more objective in terms of algorithm. Your form class should include a public var that gets increased/decreased when a form or button get a click.
Have you listened <b>Cocteau twins?</b>
- carvalho32
- Posts: 46
- Joined: Fri Jun 25, 2004 7:40 pm
- Location: Brazil
5 posts
• Page 1 of 1
Return to HTML, CSS, and Scripts
Who is online
Users browsing this forum: No registered users and 5 guests