Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control LOSTFOCUS not triggered when closing form via 'X'

Status
Not open for further replies.

SteveShanks

Programmer
Nov 28, 2001
112
GB
Hi

I have a form which sets a changed flag on an object whenever the user changes a field and moves off it (handled in the LostFocus event). This flag is then checked in the QueryUload event of the form.

It would appear that the lostfocus event on a textbox is not fired if you enter a value into the control and then click the 'X' on the form to close it.

Is there a solution to this?

Thanks
Steve
 
Catch it On "Unload Event" of the form.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Education's purpose is to replace an empty mind with an open one.
~Malcolm S. Forbes~
 
Is there a reason the check is in the LostFocus event? Could you not set the flag in the Change event of the text box
?
 
This is correct behavior. Just don't expect that a control loses focus when you close the form via "x".
 
Well,
The following works. It is not a good way (i think), but.. it works:

Code:
Private Sub Form_Unload(Cancel As Integer)
    Cancel = True       'not exit
    SendKeys "{TAB}"    'change focus to trigger the LostFocus event
    DoEvents            ' Try not putting it !!!
    Cancel = False      ' allow to exit
    
    ' your code here.....
End Sub


Hope this "trick" helps
 
Why not disable the x?

'Load form
'Disable 'x'
RemoveMenu GetSystemMenu(Me.hwnd, 0), 6, MF_BYPOSITION


'Module
'Disable closing by 'x'
Public Const MF_BYPOSITION = &H400
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long



 
SteveShanks,

Will this be helpful?

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If UnloadMode = vbFormControlMenu Then
MsgBox "Last Control in focus was " & LastControl
End If
End Sub

Private Function LastControl() As String
LastControl = Me.ActiveControl.Name
End Function

vladk
 
Thanks for the replies...currently looking at something similar to that just suggested by vladk

Thanks again everyone
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top