When is lostfocus called? According to my prog its never!
Printable View
When is lostfocus called? According to my prog its never!
Let's say you have two textboxes, Text1 and Text2. If you click on Text1, Text1 will get the focus. If you then click on Text2, Text1 loses the focus. So, the LostFocus event of Text1 will be fired.
In other words, when the control no longer has the focus.
You said it never occurs? Which control are you using and testing it with?
Here's a small example I made. Add the following code to a Form with 3 TextBoxes (Text1, Text2, Text3) and 1 PictureBox (Picture1).
A MessageBox will pop up when a control loses focus.Code:Private Sub Text1_LostFocus()
MsgBox "Text1 lost focus"
End Sub
Private Sub Text2_LostFocus()
MsgBox "Text2 lost focus"
End Sub
Private Sub Text3_LostFocus()
MsgBox "Text3 lost focus"
End Sub
Private Sub Picture1_LostFocus()
MsgBox "Picture1 lost focus"
End Sub
So how does this work for a form?
I have this code in form1_lostfocus
MsgBox "It lost focus!"
And no matter what I do, it never gets called!
For a form, you will have to have two forms and click one and the other will lose the focus. But what you probably want to do is know when your program has lost focus. You can subclass to do this.
Can't remember who this code is from.
It's located somewhere on Vb-World.
Great code to whoever gave it! :rolleyes:
Code:Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal ndx As Long, ByVal newValue As Long) As Long
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
' This is used with the SetWindowLong API function.
Const GWL_WNDPROC = -4
Public Const WM_KILLFOCUS = &H8
Dim saveHWnd As Long ' The handle of the subclassed window.
Dim oldProcAddr As Long ' The address of the original window procedure
Sub StartSubclassing(ByVal hWnd As Long)
saveHWnd = hWnd
oldProcAddr = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Sub StopSubclassing()
SetWindowLong saveHWnd, GWL_WNDPROC, oldProcAddr
End Sub
Function WndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
' Send the message to the original window procedure, and then
' return Windows the return value from the original procedure.
WndProc = CallWindowProc(oldProcAddr, hWnd, uMsg, wParam, lParam)
Select Case uMsg
Case WM_KILLFOCUS
'Form has Lost Focus
Form1.WindowState = 1
End Select
End Function
Private Sub Form_Load()
StartSubclassing Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
StopSubclassing
End Sub
Lost Focus Generally implies a control/window etc has had focus, the LostFocus event can then be used to test for conditions etc.
Hope that helps.Code:
Procedure TxtName.LostFocus()
if len(trim(txtname)) = 0 then
MsgBox "Name is a required Entry"
TxtName.SetFocus
End If
The GetLost event of the form... er... I mean the LostFocus event,
is about as usefull as a wooly blanket in summer.
The LostFocus event of any control happens when the the control
has the focus, and then loses it. Forms are notorious for not
ever getting the focus, that's why they don't lose the focus.
The only time a form will ever get the focus is if it does not
contain any controls that can have the focus. So if you have
a form with no buttons and no textboxes, in other words a pretty
useless form, the form might get the focus. Then if something
else gets the focus, the form loses it and the LostFocus event occurs.
If you have buttons and stuff on the form though, they will get
the focus instead, and the form will just be there for fun. Even
when you click on the form, it will not get the focus.
Hope this helps.
Shrog
[Edited by Shrog on 11-27-2000 at 04:52 AM]
Generally put any Lost style code in the form.unload event. Closing dbs, checking settings etc. Have never actually run across any code to do with a form.LostFocus event.