I don't understand this behavior...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dang, default button - sorry for the 'blank' post I just sent. I wanted to
'subscribe' to the format and parse events for a binding for a control on my
form. The binding was set up in the IDE, so I had thought I could do this....

Dim b As Binding = RenewalDateDateTimePicker.DataBindings.Item("Value")
AddHandler b.Format, AddressOf OnRenewalDateBindingFormat
AddHandler b.Parse, AddressOf OnRenewalDateBindingFormat

but, the event handler was never called.

So, I removed the binding from the Ide and did this...
Dim b As Binding = New Binding("Value", SubscriptionBindingSource,
"RenewalDate", True)
AddHandler b.Format, AddressOf OnRenewalDateBindingFormat
AddHandler b.Parse, AddressOf OnRenewalDateBindingFormat
RenewalDateDateTimePicker.DataBindings.Add(b)

And that worked fine. But I discovered that if I changed the order to this...
Dim b As Binding = New Binding("Value", SubscriptionBindingSource,
"RenewalDate", True)
RenewalDateDateTimePicker.DataBindings.Add(b)
AddHandler b.Format, AddressOf OnRenewalDateBindingFormat
AddHandler b.Parse, AddressOf OnRenewalDateBindingFormat

Then the event handler is never called, again. In other words, you don't
seem to be able to add an eventhandler to a binding class event, after the
'binding' has been added to the DataBindings collection for the control. Is
this by design?
 
Hi Terry,

I think you have some misunderstandings here.

The Binding.Format event is raised when data is pushed from the data source
into the control. You can handle the Format event to convert unformatted
data from the data source into formatted data for display. When data is
pulled from the control into the data source, the Parse event is raised to
unformat the displayed value, then the Format event occurs to reformat the
data for display.

In your scenario, the data source already has data in it before you bind it
to the Value property of a DateTimePicker and you subscribe the Format
event of the Binding object after the data source is bound to the control,
i.e. the Binding object has been added into the DataBindings collection of
the DateTimePicker.

In fact, the Format event is indeed raised when you bind the data source to
the DateTimePicker because the first data item in the data source is pushed
from the data source to the DateTimePicker, but the handler has not been
hooked up to the Format event at this time, so this handler is not called.

If you navigate through the data source, you should see the Format event
handler is called.

So to solve your problem, fill data into the data source AFTER you
subscribe the Format and Parse events of the Binding object.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Linda,
Thanks! You were right, I was setting the datasource before I had
subscribed to the event. It now works perfectly!
Thanks for all you help.
 
Back
Top