Results 1 to 6 of 6

Thread: Form_GotFocus not firing..

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Form_GotFocus not firing..

    If i switch to another window then back to my form, the gotfocus isnt firing..
    any idea why?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Form_GotFocus not firing..

    Try the Activate event instead.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    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
    Rob C

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width