gpt4 book ai didi

vb.net - 取消日期时间选择器 ValueChanged 事件

转载 作者:行者123 更新时间:2023-12-01 15:50:05 27 4
gpt4 key购买 nike

有没有办法通过条件取消事件?我试过 e.cancel但它不起作用。取消事件后dtpAudit_From.Value必须恢复到原来的值。

Private Sub dtpAudit_From_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpAudit_From.ValueChanged
'check if two DTPs (Date time pickers) are valid
If dtpAudit_From.Value > dtpAudit_To.Value Then
MsgBox("cancel the event")

End If
End Sub

最佳答案

一种方法是子类化 DateTimePicker并添加一个 ValueChanging事件。下面是一个例子:

Public Class UIDateTimePicker
Inherits DateTimePicker

Public Sub New()
Me.cachedValue = Me.Value
End Sub

Public Event ValueChanging As CancelEventHandler

Protected Overrides Sub OnValueChanged(e As EventArgs)
If (Not Me.reverting) Then
Dim evargs As New CancelEventArgs(False)
Me.OnValueChanging(evargs)
If ((Not evargs Is Nothing) AndAlso evargs.Cancel) Then
Dim value As Date = Me.Value
Me.reverting = True
Me.Value = Me.cachedValue
Else
Me.cachedValue = Value
MyBase.OnValueChanged(e)
End If
Me.reverting = False
End If
End Sub

Protected Overridable Sub OnValueChanging(e As CancelEventArgs)
RaiseEvent ValueChanging(Me, e)
End Sub

Private cachedValue As DateTime
Private reverting As Boolean

End Class

用法
Private Sub dtpAudit_From_ValueChanging(sender As Object, e As CancelEventArgs) Handles dtpAudit_From.ValueChanging
e.Cancel = {Condition}
End Sub

关于vb.net - 取消日期时间选择器 ValueChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com