Home All Groups Group Topic Archive Search About

VB2005 - Bound Date TextBox Issues

Author
5 Jan 2006 11:13 PM
Matt
I have a number of TextBox controls bound to date fields in my
application, and have come across a disconcerting issue: When you are
entering data into the bound field, if you enter any string that cannot
be converted by the system into the date/time format, the application
will not allow you to leave the TextBox control, even to close the
window. It doesn't throw up an error or provide any feedback, you simply
cannot click or tab out of the control until you input a valid string
that may be converted to a date/time. While I can understand the need to
get proper values into the field, it makes no sense to me that you would
make the user simply unable to exit the field, and display no feedback
whatsoever.

Worse, however, is the fact that, once you have entered any value into
the field, you cannot delete that value (i.e., return the TextBox to an
empty state). I would assume this is due to the fact that it does not
accept "" (null string) as a valid entry for date/time values, but it
still causes innumerable headaches, and should really be handled
properly by the control.

Has anyone found an effective way to get around this issue, and can
anyone explain why Microsoft chose to code the controls in this manner?

Thanks,
Matt

Author
6 Jan 2006 8:30 AM
Kevin Yu [MSFT]
Hi Matt,

You can handle the parse validations in the Binding.Parse event like the
following:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        Me.SqlDataAdapter1.Fill(Me.DataSet11.Orders)
        AddHandler Me.TextBox1.DataBindings(0).Parse, AddressOf aaa
    End Sub

    Private Sub aaa(ByVal sender As Object, ByVal e As ConvertEventArgs)
        Try
            DateTime.Parse(Me.TextBox1.Text)
        Catch ex As Exception
            If Me.TextBox1.Text <> String.Empty Then
                Dim pm As PropertyManager =
CType(Me.TextBox1.BindingContext(Me.DataSet11, "Orders.RequiredDate"),
PropertyManager)
                e.Value = pm.Current
            Else
                e.Value = DBNull.Value
            End If
        End Try
    End Sub

Here I rolled the value back to the original value if the datetime is not
valid. Or if we enter an empty string, it leaves it as DBNull.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Author
6 Jan 2006 9:15 AM
Cor Ligthert [MVP]
Matt,

Have a look at the binding events.

http://www.vb-tips.com/default.aspx?ID=c4832a2a-2b95-4ded-93d9-4deb7fa4a0b8

I hope this helps,

Cor