Mouse Click / Tab

So basically you don't really use the validation tools (events) available, the Validated event is where you do stuff after the control has validated. Do the validation in the Validating event, set Cancel=True if the routine determines that the data doesn't validate.
 
right got ya. I've never really done anything with the validation events on previous apps, this is the first time I've used them. Obviously got things the wrong way round!!

I've now changed it to this, and it works fine. Would you agree that this now looks OK?

VB.NET:
Private Sub txtMoistNIR_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMoistNIR.Validating
        strMoistMin = Me.txtMinNIR.Text
        strMoistMax = Me.txtMaxNIR.Text
        Me.MositureRejectActions()

        If String.IsNullOrEmpty(txtMoistNIR.Text.Trim()) Then
            e.Cancel = True
            txtMoistNIR.Focus()
        Else
            Dim intValue As Decimal = CDec(Me.txtMoistNIR.Text)
            If intValue <= decRejectLow Or intValue >= decRejectHigh Then
                'Me.RejectIt()
                MessageBox.Show("this was rejected")
            ElseIf (intValue >= decActionLowMin AndAlso intValue <= decActionLowMax) Or (intValue >= decActionHighMin AndAlso intValue <= decActionHighMax) Then
                'Me.ActionIt()
                MessageBox.Show("this was actioned")
            End If
            intValue = Nothing
        End If
    End Sub

    Private Sub txtMoistNIR_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMoistNIR.Validated
        Me.txtMoistSar.Focus()
    End Sub
 
How come you're setting intValue to Nothing? The GC will take care of that for you.
 
No, because you have validation in the Else part and don't cancel if it's wrong. Perhaps something like this:
VB.NET:
Private Sub txtMoistNIR_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMoistNIR.Validating
    Dim decValue As Decimal
    If Decimal.TryParse(txtMoistNIR.Text, decValue) Then
        If decValue <= decRejectLow Or decValue >= decRejectHigh Then
            'Me.RejectIt()
            MessageBox.Show("this was rejected")
            e.Cancel = True
        End If
    Else
        e.Cancel = True
    End If
End Sub
For other cases this will pass to Validated event. ActionIt() if that still belongs would probably be put there.
 
How come you're setting intValue to Nothing? The GC will take care of that for you.

thanks for pointing that out, i'd put it in for testing to make sure it didn't affect my other textbox validations.


No, because you have validation in the Else part and don't cancel if it's wrong. Perhaps something like this:

I understand what you've done - unfortunately I didn't explain myself properly the first time round - there's also a range where the value is accepted - that's why I set a range for Action and a range for Reject in the code. Any other value would mean it falls into the "acceptable" range, and the application then allows for the next textbox to be filled in.
 
Last edited:
Just make the easiest comparison to determine if you must set Cancel=True, that's what stops the control from validating.
 
I have a suggestion. Instead of seeing weather the user entered in a decimal, why not only allow them to enter a decimal?

Also, you're wasting precious memory!

VB.NET:
    Dim decValue As Decimal
    If Decimal.TryParse(txtMoistNIR.Text, decValue) Then

^ Change that to this :

VB.NET:
If Decimal.TryParse(txtMoistNIR.Text, Decimal) Then

...Or just do what I said above and only allow them to enter a decimal.
 
I'm afraid you don't know how the TryParse method works. You have to catch the parsed value as my example, the variable you need anyway so no memory is wasted. Why would you discard this already parsed value and parse it again? You're wasting precious CPU cycles! But you are right the NumericUpDown control can be used instead of a Textbox.

Edit: I just checked your code again, it won't even compile, so there is no discussion about it.
 
Last edited:
Back
Top