|
-
Sep 19th, 2006, 08:23 PM
#1
Thread Starter
Junior Member
[RESOLVED] Validation on multiple fields?
Im trying to validate all the fields before I enable the calcButton. The issue I'm having is that the errorMessage doesn't clear once i enter a correct value. Hence the calcButton is disabled. How can I clear the ErrorProvider once the validation is good?
This code is probably not the write way to do it. Any advice on a better way to code it would be greatly appreciated, and if you have some sample coding that would be icing on the cake.
Thanks again.
VB Code:
Private Sub SetFormErrorState()
Dim errorMessage As String = Nothing
'Checking CC# for numeric
If Not Me.ValidateTextBox(Me.phoneTextBox.Text) Then
'The user has not entered the required value.
errorMessage = "Entry Required"
'Checking phone for numeric
Me.ErrorProvider.SetError(Me.phoneTextBox, errorMessage)
ElseIf Me.ccTextBox.Enabled AndAlso Not Me.ValidateTextBox(Me.ccTextBox.Text) Then
'The user has not entered the required value.
errorMessage = "Entry Required"
Me.ErrorProvider.SetError(Me.ccTextBox, errorMessage)
ElseIf Not Me.ValidateTextBox(Me.startMiTextBox.Text) Then
'The user has not entered the required value.
errorMessage = "Entry Required"
Me.ErrorProvider.SetError(Me.startMiTextBox, errorMessage)
ElseIf Not Me.ValidateTextBox(Me.endMiTextBox.Text) Then
'The user has not entered the required value.
errorMessage = "Entry Required"
Me.ErrorProvider.SetError(Me.endMiTextBox, errorMessage)
ElseIf Not Me.ValidateTextBox(Me.daysRentedTextBox.Text) Then
'The user has not entered the required value.
errorMessage = "Entry Required"
Me.ErrorProvider.SetError(Me.daysRentedTextBox, errorMessage)
Else
Me.ErrorProvider.Clear()
End If
'Enable the OK button if and only if there are no errors.
Me.calculateButton.Enabled = (errorMessage Is Nothing)
End Sub
Private Function ValidateTextBox(ByVal varTextBoxString As String) As Boolean
If varTextBoxString = "" Then
Return False
Else
Return True
End If
'Return True if the TextBox contains any value, otherwise return False.
End Function
Last edited by mrman99; Sep 19th, 2006 at 10:30 PM.
-
Sep 19th, 2006, 08:36 PM
#2
Re: Validation on multiple fields?
You need to handle the error for each TextBox separately and each time the text changes. This works as you'd expect:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetErrorState()
End Sub
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged, _
TextBox2.TextChanged, _
TextBox1.TextChanged
Me.SetErrorState()
End Sub
Private Sub SetErrorState()
Const ERR_MSG As String = "Required field"
Dim errCount As Integer = 0
If Me.ValidateNonEmptyString(Me.TextBox1.Text) Then
Me.ErrorProvider1.SetError(Me.TextBox1, Nothing)
Else
Me.ErrorProvider1.SetError(Me.TextBox1, ERR_MSG)
errCount += 1
End If
If Me.ValidateNonEmptyString(Me.TextBox2.Text) Then
Me.ErrorProvider1.SetError(Me.TextBox2, Nothing)
Else
Me.ErrorProvider1.SetError(Me.TextBox2, ERR_MSG)
errCount += 1
End If
If Me.ValidateNonEmptyString(Me.TextBox3.Text) Then
Me.ErrorProvider1.SetError(Me.TextBox3, Nothing)
Else
Me.ErrorProvider1.SetError(Me.TextBox3, ERR_MSG)
errCount += 1
End If
Me.Button1.Enabled = (errCount = 0)
End Sub
Private Function ValidateNonEmptyString(ByVal str As String) As Boolean
Return str.Trim().Length > 0
End Function
There are ways to reduce the code but that's the general idea. The more TextBoxes that have different validation requirements, such as the one that might be disabled, the less you could reduce the code.
Last edited by jmcilhinney; Sep 19th, 2006 at 08:41 PM.
-
Sep 19th, 2006, 09:43 PM
#3
Thread Starter
Junior Member
Re: Validation on multiple fields?
VB Code:
Private Sub PaymentTypeComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PaymentTypeComboBox.SelectedIndexChanged
'disable the ccTextBox if "Cash" is selected
Me.ccTextBox.Enabled = (CStr(Me.PaymentTypeComboBox.SelectedItem) <> "Cash")
End Sub
Private Sub ccTextBox_PropertyChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ccTextBox.EnabledChanged, ccTextBox.TextChanged
'Clear the TextBox if it is disabled.
If Not Me.ccTextBox.Enabled Then
Me.ccTextBox.Clear()
End If
'Set the properties of the OK button and ErrorProvider appropriately for the state of the TextBox.
Me.SetFormErrorState()
End Sub
Private Sub SetFormErrorState()
Const ERR_MSG As String = "Entry Required"
Dim errCount As Integer = 0
If Me.ValidateNonEmptyString(Me.phoneTextBox.Text) Then
Me.ErrorProvider.SetError(Me.phoneTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.phoneTextBox, ERR_MSG)
errCount += 1
End If
If Me.ccTextBox.Enabled AndAlso Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
errCount += 1
End If
If Me.ValidateNonEmptyString(Me.startMiTextBox.Text) Then
Me.ErrorProvider.SetError(Me.startMiTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.startMiTextBox, ERR_MSG)
errCount += 1
End If
If Me.ValidateNonEmptyString(Me.endMiTextBox.Text) Then
Me.ErrorProvider.SetError(Me.endMiTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.endMiTextBox, ERR_MSG)
errCount += 1
End If
If Me.ValidateNonEmptyString(Me.daysRentedTextBox.Text) Then
Me.ErrorProvider.SetError(Me.daysRentedTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.daysRentedTextBox, ERR_MSG)
errCount += 1
End If
If errCount = 0 Then
Me.ErrorProvider.Clear()
End If
'Enable the OK button if and only if there are no errors.
Me.calculateButton.Enabled = (errCount = 0)
End Sub
everything works great, but when the ccTextBox is disabled, the error still shows up "Entry Required!"
VB Code:
If Me.ccTextBox.Enabled AndAlso Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
errCount += 1
End If
Is this part look right?
-
Sep 19th, 2006, 09:53 PM
#4
Re: Validation on multiple fields?
No it doesn't. Ask yourself, when do you want to display an error on ccTextBox and when do you not, then ask yourself whether the condition you've used will do that. Where will that code go if the Enabled property is False?
-
Sep 19th, 2006, 10:29 PM
#5
Thread Starter
Junior Member
Re: Validation on multiple fields?
VB Code:
If Me.ccTextBox.Enabled AndAlso Not Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
errCount += 1
Else
Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
End If
changed it to the above to make it work right
-
Sep 19th, 2006, 10:33 PM
#6
Re: [RESOLVED] Validation on multiple fields?
Not that there's anything wrong with that but for future reference note that the following would also work:
VB Code:
If Not Me.ccTextBox.Enabled OrElse Me.ValidateNonEmptyString(Me.ccTextBox.Text) Then
Me.ErrorProvider.SetError(Me.ccTextBox, Nothing)
Else
Me.ErrorProvider.SetError(Me.ccTextBox, ERR_MSG)
errCount += 1
End If
If you weren't going to use an Else block then that might be preferable. As you are using an Else block neither is any better than the other.
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
|