Any way to intercept the Format() and Parse() methods for all bound objects on a form?

P

Pritcham

Hi

If there are a number of textboxes (e.g.) on a form that are manually
bound to an object (by using textbox.DataBindings.Add("Text", object,
"PropertyToBind")) is there any way of intercepting all of the Format()
and Parse() events that occur for each of these textboxes (so I can do
some checking for nulls and reformat etc accordingly) or would I have
to implement the OnParse() OnFormat handlers for each textbox?

Thanks in advance
Martin
 
S

Sericinus hunter

Pritcham said:
Hi

If there are a number of textboxes (e.g.) on a form that are manually
bound to an object (by using textbox.DataBindings.Add("Text", object,
"PropertyToBind")) is there any way of intercepting all of the Format()
and Parse() events that occur for each of these textboxes (so I can do
some checking for nulls and reformat etc accordingly) or would I have
to implement the OnParse() OnFormat handlers for each textbox?

You can have one handler for all, but still (I think) need to
subscribe to the corresponding event for each binding.

Binding b = new Binding("Text", object, "PropToBind");
b.Format += new ConvertEventHandler(your_format_handler);
textboxONE.DataBindings.Add(b);

b = new Binding("Text", object, "PropToBind");
b.Format += new ConvertEventHandler(your_format_handler);
textboxTWO.DataBindings.Add(b);
 
J

Jim Wooley

You could subclass the textbox and add the additional functionality there.
Alternatively, why not do the processing in the business object itself. It
sounds like you are trying to enforce business rules in the UI.

Additionally, you may want to look more at the DataBinding.Add overloads
as you can specify the NullValue and Formatting handling there.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
P

Pritcham

Hi

Thanks for the replies - much appreciated.

Jim, I must admit that I'd thought of subclassing already but wanted to
see whether there was anyway of doing a catch-all for the event rather
than replacing the existing textboxes. I'm not really trying to
validate any business rules in the UI as these are already
validated/enforced in the BLL. I was actually trying to deal with the
Null exceptions that arise sometimes when binding to nullable
properties in the entity - I don't want to change the value held (from
a null to a String.Empty for example), just how it's displayed. I'll
look at the overloads though, I must admit I didn't realise there were
any options for NullValue and formatting (that'll teach me for looking
for a more complex solution to a common problem!).

Cheers again
Martin
 

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