VB2005 - Bound Date TextBox Issues

M

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
 
K

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top