binding format

  • Thread starter Thread starter Michael Roberts Jr
  • Start date Start date
M

Michael Roberts Jr

I am having trouble with a binding that was created to format a textbox on a
form. I have created a format procedure that takes a date and makes a
string, so that it can be displayed as a short date.

Private Sub DatetoShort(ByVal sender As Object, ByVal cevent As
ConvertEventArgs)
' The method converts only to string type. Test this using the DesiredType.

If Not cevent.DesiredType Is GetType(String) Then

Exit Sub

End If

' Use the ToshortDate method to format short date

Try ' if date is null then catch

cevent.Value = CType(cevent.Value, Date).ToShortDateString

Catch

End Try

End Sub


The problem I have is that I can't figure out how to get the value to go to
null if the date needs to be erased. For example, a date of birth might not
be available. If it is not entered into the database with a new record then
there is no problem. But then if I change the value and give it a date. I
can't erase the date later. The code for the textbox is as follows:
' Create the binding date of birth

Dim bBirthdate As Binding = New Binding _

("Text", Me.objAlphaStudent, "DERecords.birthdate")

' Add the delegates to the event

AddHandler bBirthdate.Format, AddressOf DatetoShort

txtbDOB.DataBindings.Add(bBirthdate)

If I try to reset the textbox.text property to "" it just reverts back to
whatever date was entered and accepted. What can I do to erase the field
(set it to null) after a valid date has been entered?
 
Add a parse event handler, something like this:

Dim bBirthdate As Binding _
= New Binding ("Text", Me.objAlphaStudent, "DERecords.birthdate")

AddHandler bBirthdate.Format, AddressOf DatetoShort
txtbDOB.DataBindings.Add(bBirthdate)

Private Sub DatetoShort(ByVal sender As Object, _
ByVal cevent As ConvertEventArgs)

If cevent.Value = "" Then
cevent.Value = DBNull.Value
Else
cevent.Value = DateTime.Parse(cevent.Value)
End If

End Sub

Per-Frode Pedersen
 
Back
Top