Solved

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

Posted on 2013-06-05
7
854 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing Version of .NET in Windows 10 17 128
Run software updates from the website 6 50
C# LINQ query question 11 32
SQL State HYT00. Timeout expired proplem 8 44
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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