English 中文(简体)
如何制止对商业物体的习惯控制,不必要地更新?
原标题:How to stop a custom control bound to a business object from updating unnecessarily?

我有一个视窗申请表,对进入日期进行海关控制。 这从文本箱中继承,对基本商业目标具有约束性,并落实价值财产。

一切都行之有效,除非控制证明它更新了受约束的财产,即使它有变。 这是一个问题,因为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

问题回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签