Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
how can i know if my application is Focused or not
Posted
Comments
joshrduncan2012 15-May-13 9:56am    
What application? Please elaborate more. This question is too vague.
Sergey Alexandrovich Kryukov 15-May-13 10:13am    
Not too vague, just incorrect. Please see the solutions.
—SA
Mohammed Hameed 15-May-13 10:00am    
Yes, specify more details.
Sergey Alexandrovich Kryukov 15-May-13 10:14am    
Only UI library should be specified. The question is just incorrect. Please see the solutions.
—SA
Mohammed Hameed 15-May-13 10:17am    
Okay, thanks.

You can check ContainsFocus[^] on all of your forms. (Assuming WinForms, because you didn't specify. If you're referring to WPF or ASPX or something else the answer may be different.)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-May-13 10:13am    
5ed. I added some explanation in my answer, please see. This solution is of course credited.
—SA
In addition to the correct Solution 2:

An application cannot be "focused" or not.

"Focus" always means keyboard focus, when a control, only one at a time per interactive Windows session, can accept user input from a keyboard. You can only determine is the control presently in focus is owner by on of the windows of your application, as Solution 2 tells you. As the the window (form), it can be activated or not. When a window is activated, the control which was previously in focus, automatically gains the focus.

—SA
 
Share this answer
 
Use the Activated and Deactivate events of your main form...

Something like this:

VB
Public Class MainForm

    Private hasFocus As Boolean = False

    Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
        hasFocus = True
    End Sub

    Private Sub MainForm_Deactivate(sender As Object, e As EventArgs) Handles MyBase.Deactivate
        hasFocus = False
    End Sub

End Class


Good luck!
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 15-May-13 10:04am    
Very redundant. No need to do that, not at all. See solution 2.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900