我有一个视窗申请表,对进入日期进行海关控制。 这从文本箱中继承,对基本商业目标具有约束性,并落实价值财产。
一切都行之有效,除非控制证明它更新了受约束的财产,即使它有变。 这是一个问题,因为Im利用实体框架和实体变更财产造成数据库的相应领域,每当用户开放并关闭这种控制的形式时,都要更新。
该守则是:
Public Class TextBoxDate
Inherits TextBox
Public ValueChanged As EventHandler
Private _dateValue As Nullable(Of Date) = Nothing
<Bindable(True)> _
<Category("Appearance")> _
Public Property Value() As Nullable(Of Date)
Bind to the Value property instead of the Text property, as the latter will not allow
the user to delete the contents of the textbox. The Value property provides support for nulls.
Get
Value = _dateValue
End Get
Set(ByVal value As Nullable(Of Date))
_dateValue = value
Update the text in the textbox
If value.HasValue Then
Text = CDate(value).ToShortDateString
Else
Text = vbNullString
End If
OnValueChanged()
End Set
End Property
Private Sub OnValueChanged()
If (ValueChanged IsNot Nothing) Then
ValueChanged(Me, New EventArgs())
End If
End Sub
Private Sub TextBoxDate_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
_userEntered = True
End Sub
Private Sub TextBoxDate_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
_userEntered = False
End Sub
Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
If _userEntered Then
If Me.TextLength = 0 Then
Null value will be saved to the database via the bound Value property
If Value IsNot Nothing Then
Value = Nothing
End If
Else
Dim dateValue As Date
If Date.TryParseExact(Text, "d/M/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, Globalization.DateTimeStyles.None, dateValue) Then
If Value Is Nothing Or (dateValue <> Value) Then
Value = CDate(Text)
End If
Else
e.Cancel = True
End If
End If
End If
MyBase.OnValidating(e)
End Sub
End Class
这使我mad。 任何帮助都会受到高度赞赏。
Scott