detect if form has the focus

Posted on 2003-02-24
Medium Priority
Last Modified: 2010-04-07

Is there a way to check if a form has the focus ..

I have made a skin OCX for my applications .. I need the skin to be in different color scheme whenever the form has the focus.

Question by:tocos
LVL 18

Assisted Solution

Sethi earned 50 total points
ID: 8012925
Screen.ActiveForm.Name returns the name of the form that has the focus in an application.
LVL 14

Accepted Solution

Esopo earned 50 total points
ID: 8012987

You can also use GetFocus from the API:

Declare Function GetFocus Lib "user32" Alias "GetFocus" () As Long

This will give you the handle of the window with the "keyboard focus" meaning the window that currently receives keyboard strokes.

Best regards.

Author Comment

ID: 8021288

- Using Screen.ActiveForm.Name on my skin returns error.
- I used GetFocus in a timer control to detect the state of form as follow:

    If GetFocus = UserControl.Parent.hWnd Then
        Shape1.BackColor = vbRed
        Shape1.BackColor = vbWhite
    End If

It worked fine in design time but when I run the application it gave getfocus value different than the .hwnd


Expert Comment

ID: 8901187
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?

Featured Post

Independent Software Vendors: 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

615 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