Solved

Determine if a WPF window with multi user controls has focus or not?

Posted on 2013-06-05
7
814 Views
Last Modified: 2013-07-05
I have a window with 3 user controls on it.  My main goal is to determine if the window or any of its controls has focus and also when that changes.

I am trying to do this with the Activated and Deactivated events of the window.

I have a problem with them firing reliably or at all.  But first does any one have a better idea.

I want to receive an event when the window or any control receives focus and also an event when the user clicks on something other than my app.

Here is my failing / unreliable attempt.

Declared
Private dbBar_ActivatedEvent As System.EventHandler
    Private dbBar_DeActivatedEvent As System.EventHandler

Open in new window


After initializeComponent
 dbBar_ActivatedEvent = New System.EventHandler(AddressOf DBBar_Activated)
        dbBar_DeActivatedEvent = New System.EventHandler(AddressOf DBBar_Deactivated)

Open in new window


Then adding or removing looks like this
            AddHandler Me.Activated, dbBar_ActivatedEvent
            AddHandler Me.Deactivated, dbBar_DeActivatedEvent
      
OR

            RemoveHandler Me.Activated, dbBar_ActivatedEvent
            RemoveHandler Me.Deactivated, dbBar_DeActivatedEvent

Open in new window


The two events
 Private Sub DBBar_Deactivated(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub DBBar_Activated(ByVal sender As Object, ByVal e As System.EventArgs) 

    End Sub

Open in new window


The strange things I am seeing are things like.  They fire while debugging with Visual studio but if running the .exe they do not.  

One of the controls has a textbox and if you click this text box some times the activated event fires and sometimes not.
0
Comment
Question by:darbid73
  • 4
  • 3
7 Comments
 
LVL 32

Expert Comment

by:sarabande
ID: 39225198
the events you should look for is setfocus and killfocus.

i found the following site which perhaps could help you further on this:

http://msdn.microsoft.com/en-us/library/aa969768.aspx#Logical_Focus

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 39225200
the activate and deactivate only would fire if last focus/new focus was another window or application.

Sara
0
 
LVL 19

Author Comment

by:darbid73
ID: 39225646
The link looks like it refers to a concept that might work but how is it implemented?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 32

Expert Comment

by:sarabande
ID: 39226209
the WM_SETFOCUS and WM_KILLFOCUS are windows messages which could be handled by a parent window like a dialog. i browsed and it seems that you can handle those messages by overriding the FilterMessage function in WPF. i hope you may find some helpful code samples if you search for WPF and FilterMessage.  

Sara
0
 
LVL 19

Accepted Solution

by:
darbid73 earned 0 total points
ID: 39277342
Thank you sarabande for your information.  I am not sure that really helps me solve my problem.

I propose the following as the answer and if there are no objections will close the question as such.

My answer is that if you have multiple controls on a window you have to deal with focus yourself.  This means you need to track focus on each element and you cannot rely on for example the window and its focus.  

My incorrect assumption going into this question was that if a textbox had focus and that textbox was in a usercontrol in another usercontrol in a window, then all of these parents would also have focus or at least receive a "gotfocus" event.  This does not seem to be the case.

This means the answer depends on what kind of controls you have and what kind of user controls you have built.  Further sometimes some elements do not actually "getFocus" thus you need to listen for, e.g. mouse clicks and then set focus else where.  This will mean that what ever did have focus will now loose focus.  By doing this you can track the focus of the window.  Once I handled that my window now has focus.  It seems that the deactivated event now works better.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 39290803
generally, the WM_SETFOCUS is sent to the window that gets the focus and the WM_KILLFOCUS is sent to the window which lost the focus. only one window can have the focus. whether the event was handled, or passed to the parent of the window, depends on on the "windows proc" that were associated to the window. you rightly recognized, that any handler (function) can actively set the focus to a control (window). however, you should see that there are a many messages involved by each user action. if multiple handlers would set focus to  acontrol, the last one wins. and all set focus before may cause some new actions. so, it is quite dangerous and mostly contra-productive to "correct" a set focus. in case of a kill focus you may check for changes made in the control that lost the focus, and if they were not valid, you may turn the focus back. however, even that can be wrong, if for example the user clicked an 'x'  button in order to cancel the current dialog.

from my experiences the set focus never should be corrected and the kill focus never should cause immediate action. instead, you could memorize the fact that a focus change happened and update validation info for the field that lost the focus. you then would evaluate this info to a later time, for instance when the ok button was clicked.

Sara
0
 
LVL 19

Author Closing Comment

by:darbid73
ID: 39301497
I see this as the solution for my specific question.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now