views:

40

answers:

1

I have a WPF form where a text box and label is present.In the "Lost Focus" event of text box, i am doing some validation and if the validation fails i will show an error message in the label and put focus back on the textbox. The problem is since i put the focus back on the text bos,the "Lost_Focus event s again fired and my validation function being called recursively and i am getting a "StackOverflow" exception

  An unhandled exception of type 'System.StackOverflowException' occurred in 
  mscorlib.dll

If i comment the line of putting cursor/focus in the textbox, everything works well. But i need the cursor to be in the textbox if validation fails

Any thoughts how to get rid of this ?

A: 

How about handling TextBox.PreviewLostKeyboardFocus instead? And in the event handler, just set "e.Handled = true" whenever the validation fails?

karmicpuppet
PreviewLostKeyboardFocus will take care of only keyboard input.my app will use a barcode scanner to read values in the textbox
Shyju
I think it should still work the same, wouldn't it? Have you tried it out?
karmicpuppet
Not it did not work
Shyju