Solved

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

Posted on 2013-06-05
7
880 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
[X]
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
  • 4
  • 3
7 Comments
 
LVL 34

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 34

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 20

Author Comment

by:darbid73
ID: 39225646
The link looks like it refers to a concept that might work but how is it implemented?
0
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
LVL 34

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 20

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 34

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 20

Author Closing Comment

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

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

622 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