Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent Validating/Validated event from firing in custom textbox - vb.net
    text
    copied!<p>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.</p> <p>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. </p> <p>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.</p> <p>I can't seem to figure out how to suppress the event. I have tried overriding the OnValidating event. That didn't work.</p> <p>Any Ideas?</p> <p>Update:</p> <p>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.</p> <pre><code> 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) &lt;&gt; 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 </code></pre> <p>Update 2:</p> <p>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):</p> <pre><code>Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs) If String.Compare(_CachedValue, Me.Text) &lt;&gt; 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload