views:

420

answers:

2

Hi,

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 valid 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.

A: 

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?

rip
Rip, I think the difference between your example and mine is that I am using a BindingSource with my custom object. The ErrorProvider is bound to the binding source like this: ErrorProvider1.DataSource = MyBindingSource. When you use this method, you don't explicitly bind an individual control as you did with TextBox1 and you don't set the ErrorProvider in any event on the control.
OneSource
A: 

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

alfred barthand