12

Specifically, I have a button that opens a non-modal child window. Something in the main window steals the focus away from the child window as it opens.

The fully general approach is to comment out parts of code until the problem goes away. I'm looking for faster methods.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
CannibalSmith
  • 4,742
  • 10
  • 44
  • 52
  • could it be as simple as another click occurred? is this a user issue or a general problem? – kenny Jul 28 '10 at 09:31

6 Answers6

27

I'm using Snoop to do this right now.

For starters, Snoop shows the current focused element and the current FocusScope in the status bar.

You can get it to show you all the GotFocus and LostFocus events:

  1. Run your app.
  2. Run Snoop.
  3. Choose your app in the dropdown.
  4. Click the binoculars ("Snoop") button.
  5. On the right pane, click the Events tab.
  6. Click to bring down the dropdown.
  7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
  8. Now do what you need to do to manipulate focus and watch the Snoop window.

Similarly you can track the FocusManager events the same way.

Magnus Lindhe
  • 7,157
  • 5
  • 48
  • 60
stone
  • 8,422
  • 5
  • 54
  • 66
  • I don't know how this is not the accepted answer. I've been using Snoop for years and somehow never knew about this functionality. Thanks! – thornhill May 11 '16 at 20:35
  • This should be accepted answer, I was getting nuts after not able to understand which element is in focus,, and this did it within seconds :) – Dreamweaver Jan 18 '18 at 17:32
  • Three years later and I had the same experience. Could not for the life of me figure out what was stealing away focus on my control. Snoop showed me in 5 seconds what it was (an obscure auto-width-setting behavior I'd long-ago attached to a combo-box on a settings page). I never would have figured that out. – Joe May 02 '19 at 14:34
0

I haven't tried this myself so can't tell exact steps, but you can try to use FocusManager.FocusedElement and FocusManager.GetFocusedElement Method to try and find the element stealing focus from your child window.

Other useful link -

Focus Overview(Have code for getting focused element): http://msdn.microsoft.com/en-us/library/aa969768.aspx#Focus_Events

akjoshi
  • 15,374
  • 13
  • 103
  • 121
0

You can try using a tool like UI Spy or Snoop to watch for the event that changes the focus. I haven't used these tools for this purpose, but I believe they can help you.

Matt
  • 4,318
  • 1
  • 27
  • 28
0

You can try to track calls to the System.Window.UIElement.Focus method (in the PresentationCore.dll) in the context of your application using Runtime Flow (developed by me).

Sergey Vlasov
  • 26,641
  • 3
  • 64
  • 66
0

Could you please mention the main operations (without going into client specific business - if it is a client project) - like is there any events hooked on to the elements, Any other operation happening on the button click - Anything which happens for any element in the parent window after the child window is shown which steals the focus. Also check any related events hooked which happen during button click event.

These are some of the ways you can start your investigation.

Anish Kurian
  • 93
  • 1
  • 2
  • 8
0

I just read about some deadlock...hope this helps (search for control.leave in msdn.microwoft.com

Caution Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers. Doing so can cause your application or the operating system to stop responding. For more information, see the WM_KILLFOCUS topic in the "Keyboard Input Reference" section, and the "Message Deadlocks" section of the "About Messages and Message Queues" topic in the MSDN library at http://msdn.microsoft.com/library.

ramnz
  • 631
  • 1
  • 6
  • 24