Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Posted on 2013-06-05
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.

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

            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.
Question by:darbid73
  • 4
  • 3
LVL 33

Expert Comment

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:


LVL 33

Expert Comment

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

LVL 20

Author Comment

ID: 39225646
The link looks like it refers to a concept that might work but how is it implemented?
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

LVL 33

Expert Comment

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.  

LVL 20

Accepted Solution

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.
LVL 33

Expert Comment

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.

LVL 20

Author Closing Comment

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

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TimeZone, day light savings, Sql server, asp.net 6 38
Where is this file? 3 26
PowerShell:  Column widths won't expand 3 18
Why use this lambda? 12 37
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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