1

For some reason focus a text box is need, so the focus is again set to the text box from its lost focus. This leads to stack over flow in the lost focus event.

Say, A Textbox and a button, now the focus is in Textbox and clicking on the button. In this case, need the focus to be in the textbox, so focus method of text box is invoked from its lost focus. This leads to stack over flow exception.

Why this happens and also how do the focus is retained in text box itself.

By handling PreviewKeyboardLostFocus of textbox, i have retained the Keyboard focus but how to retain the mouse focus in this?

Sankarann
  • 2,625
  • 4
  • 22
  • 59
  • You can call Focus() method for your TextBox when Button click is happened. Have you tried that ? – Nitesh Jul 24 '13 at 06:00
  • I need to handle things in Textbox lostfocus.. Focus should not move to button. While invoke the focus method in Textbox lostfocus its leads to stack over flow.. – Sankarann Jul 24 '13 at 06:03
  • If you do not wish Button to get focus then set Focusable="False" for your Button. – Nitesh Jul 24 '13 at 06:05
  • @Nitesh: its not about the Button. it may have any other control, i need to focus in the textbox itself.. like am validating the textbox. so i wont allow the focus to others. – Sankarann Jul 24 '13 at 06:07
  • Add Code to your post – Mohammad Jul 24 '13 at 06:16
  • 1
    See the solution to this question: http://stackoverflow.com/questions/2212999/stop-a-wpf-textbox-from-losing-focus – Richard E Jul 24 '13 at 06:25
  • The Stackoverflow is most likely caused by a cyclic call. Your method of Setting the Focus in the LostFocus Event somehow triggers the LostFocus Event again. – M C Jul 24 '13 at 06:49
  • If you don't allow to Focus the Button then the Button cannot be clicked... – H H Jul 24 '13 at 07:11
  • 2
    There is no notion of 'Mouse Focus' in WPF, just 'Keyboard Focus' and 'Logical Focus'. Check this [Focus Overview](http://msdn.microsoft.com/en-us/library/aa969768.aspx) article on MSDN. You may find that the `FocusManager.IsFocusScope` attached property mentioned on that page could help you. – Sheridan Jul 24 '13 at 11:05

1 Answers1

1

Check if you had handled SetFocus() Event, You might have handled something there leading to StackOverflowException.

Cheers!

Arun Selva Kumar
  • 2,593
  • 3
  • 19
  • 30