I'm trying to add in data validation for my program so it doesnt die when a user input the wrong type of data. eg. My program collects numbers and stores them against the selected name but if i input say fish instead of 2000 in the textbox my program dies.
Could someone let me know how to validate this.
Public Class Form1
'Declares 2 arrays with a maximum of 4 entries
Dim strtotals(3) As String
Dim strsales(3) As String
Dim intPasswordCounter As Integer = 0
Private Sub btnDisplayTotals_Click(sender As System.Object, e As System.EventArgs) Handles btnDisplayTotals.Click
'Validate the password first thing, then exit if it returns false
If Not ValidPassword Then Exit Sub
' Variables for handing data
Dim strIncomes2(3) As String
Dim sngSalary(3) As Single
Dim sngMax As Single
Dim sngSal As Single
Dim strEmployee As String
Dim strTotal(3) As String
Const Commission As Decimal = 0.015
Const Pay As Single = 400
strEmployee = ""
sngSal = 0
For i = 0 To 3
'Calculations
If strsales(i) <> 0 Then
strTotal(i) = Str(strsales(i))
sngSalary(i) = Pay + Int(strsales(i)) * Commission
Else
strTotal(i) = 0
sngSalary(i) = Pay
End If
strIncomes2(i) = Str(sngSalary(i))
If sngSalary(i) > sngSal Then
sngMax = sngSalary(i)
strEmployee = lstNames.Items(i)
End If
sngSal = sngSalary(i)
strtotals(i) = "Sales Total for " + lstNames.Items(i) + " is $" + strTotal(i) + " Salary is $" + strIncomes2(i) + vbNewLine
Next
lblMessage1.Text = strtotals(0) + strtotals(1) + strtotals(2) + strtotals(3) + "The Highest Salary was $" + Str(sngMax) + " by " + strEmployee
End Sub
Private Function ValidPassword() As Boolean
'Initialize the function to return false.
'The return value will only change if the correct password is entered
ValidPassword = False
For i = 0 To 2
Dim strPassword As String = InputBox("Please Enter a Valid Password", "Password Input")
If strPassword = "admin" Then
'Got the right password force a return with a value of True
Return True
End If
Next
'function didn't return yet, so user must have taken too many tries.
MsgBox("Exceeded maximum attempts... Ending")
End
End Function
Private Sub btnSale_Click(sender As System.Object, e As System.EventArgs) Handles btnSale.Click
'Sale(lstNames.SelectedIndex).intSales += Val(txtSales.Text)
For i = 0 To 3
If lstNames.SelectedIndex = i Then
strsales(i) = txtSales.Text 'Stores Sales data with the selected list name
txtSales.Text = ""
End If
Next
End Sub
Private Sub btnEnd_Click(sender As System.Object, e As System.EventArgs) Handles btnEnd.Click
End
End Sub
End Class