|
-
Apr 8th, 2005, 09:38 PM
#1
Thread Starter
Frenzied Member
Form_GotFocus not firing..
If i switch to another window then back to my form, the gotfocus isnt firing..
any idea why?
-
Apr 8th, 2005, 09:50 PM
#2
Re: Form_GotFocus not firing..
The event only fires if no controls have focus or enabled.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 8th, 2005, 10:01 PM
#3
Re: Form_GotFocus not firing..
Try the Activate event instead.
-
Apr 8th, 2005, 10:09 PM
#4
Fanatic Member
Re: Form_GotFocus not firing..
When you say
switch to another window then back
Do you mean anothe pgm ?
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
-
Apr 8th, 2005, 10:18 PM
#5
Re: Form_GotFocus not firing..
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 9th, 2005, 07:29 AM
#6
Re: Form_GotFocus not firing..
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
Code:
On Error Resume Next
Debug.Print 1 / 0
gDebugMode = (Err.Number <> 0)
Then you do this.
Code:
If Not gDebugMode Then Call Subclass(Me)
On form unload do this...
Code:
If Not gDebugMode Then Call Unsubclass
This goes into a module...
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
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.
This will show you how nicely this works.
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
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...
Last edited by szlamany; Feb 14th, 2006 at 05:52 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|