Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-06-05
7
Medium Priority
?
922 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 35

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 35

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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 35

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

783 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