开发者

ErrorProvider Not Allowing Blank Date TextBox Field

I'm using the ErrorProvider in VB.Net (2005) which is associated with a BindingSource that is bound to a custom object that I have created. This custom object contains a date field that has a "Date" data type that. I am using a "TextBox" to bind the date field in my form. My issue is, whenever the TextBox loses focus and is blank, "String not recognized as a vali开发者_如何学Cd DateTime" is displayed by the ErrorProvider and the focus can't be changed to any other control on the Form. It's good that the ErrorProvider validates entries on Date fields by default (I didn't set up my custom object to display this particular error for the date), but it should allow blank values. I want the user to be able to have a blank date with no error message displayed. How can this be done using a Date field bound to a TextBox?

I guess I could just change the date datatype to a string in my custom object, but would prefer not to do this. I tried setting the ErrorProvider to an empty string in the "Validating" event for the TextBox, but no luck. Thanks for any assistance.


the AutoValidate property of the form containing the control defines the behavior when validation fails
i think yours is set to "EnablePreventFocusChange", which makes impossible to change the focus from an invalid control


I managed to bind a date property to a textbox without the validation from an ErrorProvider kicking in. I dragged a Textbox and an ErrorProvider on to a form and entered the following code in the form’s code behind.

Public Class Form1

        Private _P As Person

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            _P = New Person With {.DOB = Date.Today}
            Me.TextBox1.DataBindings.Add("Text", _P, "DOB", True, DataSourceUpdateMode.OnPropertyChanged, Nothing)
    End Sub

End Class

Public Class Person

        Private _DOB As Date
            Public Property DOB() As Date
                Get
                        Return _DOB
                End Get
                Set(ByVal value As Date)
                        _DOB = value
                End Set
            End Property

End Class

I couldn't recreate the problem you were having. Maybe your line that databinds the textbox is different? or a different event on the textbox is setting the ErrorProvider? or a different control is setting the ErrorProvider?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜