Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


check if an winows is focused

Posted on 1999-07-07
Medium Priority
Last Modified: 2010-05-02
How can I check if a window in VB is focused?
Question by:evertd
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
  • 2

Expert Comment

ID: 1522496
check out Screen.ActiveForm

Returns the form that is the active window. If an MDIForm object is active or is referenced, it specifies the active MDI child form.



The object placeholder represents an object expression that evaluates to an object in the Applies To list.


Use the ActiveForm property to access a form's properties or to invoke its methods — for example, Screen.ActiveForm.MousePointer = 4.

This property is especially useful in a multiple-document interface (MDI) application where a button on a toolbar must initiate an action on a control in an MDI child form. When a user clicks the Copy button on the toolbar, your code can reference the text in the active control on the MDI child form — for example, ActiveForm.ActiveControl.SelText.

When a control on a form has the focus, that form is the active form on the screen (Screen.ActiveForm).  In addition, an MDIForm object can contain one child form that is the active form within the context of the MDI parent form (MDIForm.ActiveForm). The ActiveForm on the screen isn't necessarily the same as the ActiveForm in the MDI form, such as when a dialog box is active. For this reason, specify the MDIForm with ActiveForm when there is a chance of a dialog box being the ActiveForm property setting.

Note   When an active MDI child form isn't maximized, the title bars of both the parent form and the child form appear active.

If you plan to pass Screen.ActiveForm or MDIForm.ActiveForm to a procedure, you must declare the argument in that procedure with the generic type (As Form) rather than a specific form type (As MyForm) even if ActiveForm always refers to the same type of form.

The ActiveForm property determines the default value for the ProjectTemplate object.

Author Comment

ID: 1522497
And how can I check if the appllication is focused?

Accepted Solution

VBGuru earned 400 total points
ID: 1522498
In your form put a timer control and paste the following code and click on the form and check out:
Option Explicit

Private Declare Function GetActiveWindow Lib "user32" () As Long

Private Sub Form_Click()
Timer1.Enabled = True
End Sub

Private Sub Form_Load()
Timer1.Interval = 2000
Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
If GetActiveWindow = hWnd Then
    MsgBox "I am the active window"
    MsgBox "I'm not the active window"
End If
Timer1.Enabled = False
End Sub


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

715 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