If i switch to another window then back to my form, the gotfocus isnt firing..
any idea why?
Printable View
If i switch to another window then back to my form, the gotfocus isnt firing..
any idea why?
The event only fires if no controls have focus or enabled.
Try the Activate event instead.
When you say
Do you mean anothe pgm ?Quote:
switch to another window then back
If you do, then it will never fire.
You will need to use aome API call(s) to detect when your pgm is re-activated.
I'm sure that you need API, I'm just not sure which one(s).
PS I had your post in one of my Firefox tabs, so did not realise that someon had recently replied.
But I'm still pretty(well reasonably), sure that you wil need API
You may be able to use the GetActiveWindow API to see which form is currently active. Also, the
SetActiveWindow may be good to. Just depends on what your trying to do.
Other then those, if you need more control then you may be better off subclassing your form so you can
trap for the WM_PAINT or relevant message associated with form activation.
If you are trying to catch the activation of your app when another app is switch to and back again - this subclass works...
First, need to know if you are in the IDE - cannot subclass in the IDE - causes the IDE to crash. Got this trick from MartinLiss
Then you do this.Code:On Error Resume Next
Debug.Print 1 / 0
gDebugMode = (Err.Number <> 0)
On form unload do this...Code:If Not gDebugMode Then Call Subclass(Me)
This goes into a module...Code:If Not gDebugMode Then Call Unsubclass
And this is the function that gets called when the app gains focus or loses focus. Take out my code in this function and restore the "commented" out code that changes the FORM CAPTION to HAS FOCUS and LOST FOCUS.Code:Option Explicit
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_ACTIVATEAPP = &H1C
'm_frmHooked is the form that the subclass 'listens' in on
Private m_lPrevProc As Long, m_frmHooked As Form
Public Function WndProc(ByVal hWnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
'Let the window process messages as well
WndProc = CallWindowProc(m_lPrevProc, hWnd, wMsg, wParam, lParam)
'See what message has been sent to the window
If wMsg = WM_ACTIVATEAPP Then
'It's the activateapp message so call the sub on the form
Call m_frmHooked.AppFocus(CBool(wParam))
End If
End Function
Public Sub Unsubclass()
Debug.Print "UnSubClass"
Call SetWindowLong(m_frmHooked.hWnd, GWL_WNDPROC, m_lPrevProc)
Set m_frmHooked = Nothing
End Sub
Public Sub Subclass(ByRef r_frm As Form)
Debug.Print "SubClass"
Set m_frmHooked = r_frm
m_lPrevProc = SetWindowLong(r_frm.hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
This will show you how nicely this works.
edit: the gModal boolean tracks whether a MODAL input box is on the screen - I needed to know that because "calling" my FOCUSPILOT sub when a MODAL box was up on the screen caused me problems...Code:Public Sub AppFocus(ByVal v_bFocus As Boolean)
Dim i As Long, j As Long, k As Long, x As Long, y As Long, z As Long
Dim s1 As String, s2 As String, s3 As String, s4 As String, s5 As String
If v_bFocus Then
'App has just gained focus
'Me.Caption = "ActivateApp; has-focus with TAB...TAG=" & tabForms.SelectedItem.Tag
'Me.Caption = "ActivateApp gmodal" & gModal
If Not gModal Then
For x = 2 To Forms.Count - 1
If tabForms.SelectedItem.Tag = CStr(Forms(x).mintTabNo) Then
' Me.Caption = "ActivateApp; has-focus TXTINPUT.TAG=" & Forms(x).txtInput.Tag & " CBOINPUT.TAG=" & Forms(x).cboInput.Tag
gfrmKeyDown(Forms(x).mintTabNo) = -1
Call FocusPilot(Forms(x), 0, 0, 0, 0)
Exit For
End If
Next
End If
Else
'App has just lost focus
'Me.Caption = "ActivateApp; no-focus"
End If
End Sub