tags:

views:

97

answers:

6

Specifically, I have a button that opens a nonmodal 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.

A: 

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
A: 

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
A: 

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.

Sergey Vlasov
A: 

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
A: 

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
A: 

I can highly recommend reading this article. It comes with some source code that can help debug focus issues, I often end up using this code to help with my own focus issues.

http://www.julmar.com/blog/mark/PermaLink,guid,2974164f-97c1-4e20-85f9-416cf6bed219.aspx

Ashley Davis