Question

I have a custom text box component (inherits from system.windows.forms.textbox) that I created in vb.net (2005) that handles the input of numeric data. It works well.

I would like to suppress the validating and validated events from firing if the number hasn't changed. If a user is tabbing through the form and tabs from the text box, the validating/validated events are fired.

I was thinking that the text box could cache the value and compare it to what is listed in the text property. If they are different, then I would want the validating/validate events to fire. If they are the same, nothing is fired.

I can't seem to figure out how to suppress the event. I have tried overriding the OnValidating event. That didn't work.

Any Ideas?

Update:

Here is the custom text box class. The idea is that I want to cache the value of the text box on the validate event. Once the value is cached, the next time the user tabs through the box, the validating event will check to see if the _Cache is different from the .Text. If so that is when I would like to raise the validating event to the parent form (as well as the validated event). If the _cache is the same, then I don't want to raise the event to the form. Essentially the text box will work the same as a regular text box except that the validating and validated method are only raised to the form when the text has changed.

 Public Class CustomTextBox

#Region "Class Level Variables"
    Private _FirstClickCompleted As Boolean = False 'used to indicate that all of the text should be highlighted when the user box is clicked - only when the control has had focus shifted to it
    Private _CachedValue As String = String.Empty
#End Region

#Region "Overridden methods"
    Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
        'check to see if the control has recently gained focus, if it has then allow the first click to highlight all of the text
        If Not _FirstClickCompleted Then
            Me.SelectAll() 'select all the text when the user clicks a mouse on it...
            _FirstClickCompleted = True
        End If

        MyBase.OnClick(e)
    End Sub

    Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
        _FirstClickCompleted = False 'reset the first click flag so that if the user clicks the control again the text will be highlighted

        MyBase.OnLostFocus(e)
    End Sub

    Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)

        If String.Compare(_CachedValue, Me.Text) <> 0 Then
            MyBase.OnValidating(e)
        End If
    End Sub

    Protected Overrides Sub OnValidated(ByVal e As System.EventArgs)
        _CachedValue = Me.Text
        MyBase.OnValidated(e)
    End Sub
#End Region

End Class

Update 2:

Thanks to xpda, the solution is simple (so simple that I didn't understand it :) ). Replace OnValidating and OnValidated with (also a boolean to record the state is required):

Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
    If String.Compare(_CachedValue, Me.Text) <> 0 Then
        _ValidatingEventRaised = True
        MyBase.OnValidating(e)
    End If
End Sub

Protected Overrides Sub OnValidated(ByVal e As System.EventArgs)
    If Not _ValidatingEventRaised Then Return

    _CachedValue = Me.Text
    _ValidatingEventRaised = False
    MyBase.OnValidated(e)
End Sub
Was it helpful?

Solution

You can set a flag in the TextChanged event and use that flag to tell whether to exit at the beginning of the validation handlers.

OTHER TIPS

Try to handle the event in your control and cancel it as below.

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    e.Cancel = True
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top