防止在自定义文本框中触发验证/验证事件 - vb.net

发布于 2024-08-09 17:19:00 字数 2526 浏览 5 评论 0原文

我有一个在 vb.net (2005) 中创建的自定义文本框组件(继承自 system.windows.forms.textbox),用于处理数字数据的输入。效果很好。

如果数字没有改变,我想禁止触发验证和验证事件。如果用户通过表单和文本框中的选项卡进行切换,则会触发 validating/validated 事件。

我认为文本框可以缓存该值并将其与文本属性中列出的内容进行比较。如果它们不同,那么我希望触发验证/验证事件。如果它们相同,则不会解雇任何内容。

我似乎不知道如何抑制该事件。我尝试过重写 OnValidating 事件。那行不通。

有什么想法吗?

更新:

这是自定义文本框类。我的想法是我想在验证事件上缓存文本框的值。一旦该值被缓存,下次用户通过选项卡浏览该框时,验证事件将检查 _Cache 是否与 .Text 不同。如果是这样,那就是我想将验证事件引发到父表单(以及经过验证的事件)的时候。如果 _cache 相同,那么我不想将事件引发到表单。本质上,文本框的工作方式与常规文本框相同,只是验证和验证方法仅在文本更改时才会提升到表单。

 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

更新2:

感谢xpda,解决方案很简单(简单到我不明白:))。将 OnValidating 和 OnValidated 替换为(还需要一个布尔值来记录状态):

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦旅人picnic 2024-08-16 17:19:01

您可以在 TextChanged 事件中设置一个标志,并使用该标志来判断是否在验证处理程序开始时退出。

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.

百合的盛世恋 2024-08-16 17:19:01

尝试在您的控件中处理该事件并取消它,如下所示。

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    e.Cancel = True
End Sub

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文