|
-
Mar 1st, 2003, 04:40 PM
#1
Thread Starter
New Member
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:
Private Sub Form_Deactivate()
Me.SetFocus
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.
-
Nov 12th, 2014, 12:27 PM
#2
Lively Member
Re: VB Snippet - Keep a form from losing focus
 Originally Posted by ffejy
This will keep a form from losing focus, for example an options box or something.
VB Code:
Private Sub Form_Deactivate()
Me.SetFocus
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 ))
-
Nov 12th, 2014, 05:07 PM
#3
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
-
Nov 12th, 2014, 06:28 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|