Infinite loop in OnAfterChange event

B

Bev Kaufman

I just upgraded to InfoPath 2007, and my first disappointment was that it
still doesn't have the standard textbox formatting that would allow me to
display a formatted phone number. So today I wrote my first InfoPath
function that can take an input string and output it in the correct format.

But when I tried to implement it in the OnAfterChange event, it caused an
infinite loop because the act of loading back the formatted string is
changing the field, so that the event fires again.

I found advice in the newsgroup to use this code:
if(eventObj.IsUndoRedo || "Insert" != eventObj.Operation)
{
return;
}
but it doesn't work.
I thought of using a boolean variable to check whether the change is the
result of the function, but keeping track of that variable would be really
hairy, particularly since I have five phone fields. There doesn't appear to
be a GotFocus event to tell me to turn the boolean off.

I saw advice to use the validation rules instead of writing code, but the
function involves more than just a simple concat statement. There must be a
simpler way to do this, but right now it's got 12 lines of code.

How can I get this working?
 
G

Greg Collins

Changes in your event occur before the event finishes. So you can set a flag
(fChangingFieldX = true) and then check for this flag at the start of the
event handler (if(fChangingFieldX == true) return;). Then at the end of your
event hanlder, set the flag back to false. This will prevent the event
handler from being allowed to call itself (or process again as a result of a
change it caused).
 
G

Greg Collins

Awesome! Glad to have helped. You will use that trick a lot in your code
development.
 

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