Results 1 to 4 of 4

Thread: VB Snippet - Keep a form from losing focus

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Location
    Newton, MA, USA
    Posts
    7

    VB Snippet - Keep a form from losing focus

    This will keep a form from losing focus, for example an options box or something.

    VB Code:
    1. Private Sub Form_Deactivate()
    2.   Me.SetFocus
    3. End Sub

    Form_LostFocus only works if the form has no controls on it, so this is the much better way to do it. I don't know how to make it flash like a MsgBox does when you try to take away its focus, but this gets the job done.

  2. #2
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    89

    Re: VB Snippet - Keep a form from losing focus

    Quote Originally Posted by ffejy View Post
    This will keep a form from losing focus, for example an options box or something.

    VB Code:
    1. Private Sub Form_Deactivate()
    2.   Me.SetFocus
    3. End Sub

    Form_LostFocus only works if the form has no controls on it, so this is the much better way to do it. I don't know how to make it flash like a MsgBox does when you try to take away its focus, but this gets the job done.
    It seems this event disabled in Visual Basic 6 !

    The line 2 never executed !

    In my opinion, it's better to use Deactivate property of other controls in a form...

    For example the following code can work properly and holds the focus on the first form while typing:

    Code:
    Private Sub Text1_LostFocus()
        Me.SetFocus
    End Sub
    ffejy i hope you still alive in the world ))

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB Snippet - Keep a form from losing focus

    1) this post is over 11 years old
    2) The Deactivate event fires when the APPLICATION loses focus to ANOTHER application. LostFocus works within a form only (so if you tab to something, oh, like Word for instance), your application will deactivate and release control. But if you use the Deactivate event... but then I'm not sure I'd want an application doing that to me in the first place. If I intend to go somewhere, I should be (within reason) be able to get there.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,914

    Re: VB Snippet - Keep a form from losing focus

    Actually, a related problem I've had to wrestle down a few times is "returning to where I came from". If it's a separate form, things tend to work fairly well. However, on occasion, I want to "pop over" to some other control on a form, do something there, and then "pop back" to where I came from once that control loses focus. Sure, I could keep track of the control that just lost focus, but that means possibly tracking the "last focus" of lots of controls.

    In yet another situation, I may be bouncing between two programs, possibly sort of Automating one (but not true automation) from the other. The following is a rather complex procedure that helps me in a specific case of that. I haven't given API declarations, but I suppose I can if asked.

    Code:
    Public Sub GiveFocusBackToLastWindow(hWndMe As Long)
        ' If hWndMe is passed in, we can make sure that this program is ignored.
        ' It can be the handle to any window that's part of the program.
        ' If we don't have the focus, then nothing is done.
        Dim hProcessMe As Long
        Dim hWndTop As Long
        Dim hProcessTop As Long
        Const GW_HWNDNEXT = 2
        Const GWL_HWNDPARENT = (-8)
        '
        If hWndForeground <> hWndMe Then Exit Sub
        '
        GetWindowThreadProcessId hWndMe, hProcessMe
        '
        hWndTop = GetTopWindow(0)
        Do
            If hWndTop = 0 Then
                Exit Do ' Couldn't find it.
            End If
            If IsWindowVisible(hWndTop) Then ' Make sure it's visible.
                If IsIconic(hWndTop) = 0 Then ' It shouldn't be minimized.
                    If GetParent(hWndTop) = 0 Then ' It shouldn't have any parent window.
                        If GetWindowLong(hWndTop, GWL_HWNDPARENT) = 0 Then ' It shouldn't have an owner.
                            GetWindowThreadProcessId hWndTop, hProcessTop
                            If hProcessTop <> hProcessMe Then ' Make sure it's not anything to do with this program.
                                If Len(WindowText(hWndTop)) > 0 Then ' Windows without captions are wierd, ignore them.
                                    If WindowText(hWndTop) <> "Program Manager" Then
                                        AppActivate hProcessTop
                                        Exit Do
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
            hWndTop = GetWindow(hWndTop, GW_HWNDNEXT)
        Loop
    End Sub
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

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