Click to See Complete Forum and Search --> : LostFocus Handling


Aho
September 15th, 1999, 05:32 AM
Hi all,

I am wondering how to force execution of LostFocus Event handler before the event causing it (mouse click on an other control for example); any idea ?

Thanks
Ahmed

Lothar Haensler
September 15th, 1999, 06:23 AM
if you need this for input validation, better check out the Validate Event in VB 6!

Aho
September 15th, 1999, 09:07 AM
Actually I am using VB4 for this code !!
Any solution ?

Vlad Chapranov
September 15th, 1999, 09:56 AM
For validation purposes in VB prior to version 6 I used to use this technic:

private intFocus as Integer
private Sub Text1_LostFocus()
If IsTextValid(Text1) = false And (intFocus = -1 Or intFocus = Text1.TabIndex) then
MsgBox "Not valid data in Text1.", , "Warning!"
intFocus = Text1.TabIndex
Text1.SetFocus
else
intFocus = -1
End If

End Sub

private Sub Text2_LostFocus()
If IsTextValid(Text2) = false And (intFocus = -1 Or intFocus = Text2.TabIndex) then
MsgBox "Not valid data in Text2.", , "Warning!"
intFocus = Text2.TabIndex
Text2.SetFocus
else
intFocus = -1
End If

End Sub
private Function IsTextValid(txt as Control) as Integer
'this function analizes contents of the text property of the control (except maskedit box)
If txt.Text = "" then
IsTextValid = false
else
IsTextValid = true
End If
End Function




To test place 2 text boxes on the form with text properties = "" and try to change the focus. Until you enter anything you will not be able to do that. You can customize IsTextValid function to do everything you want.
HTH
Vlad

Lothar Haensler
September 15th, 1999, 10:02 AM
private Sub Text1_LostFocus()
If Text1.Text = "" then
MsgBox "bad: field must be filled"
DoEvents ' important
Text1.SetFocus
End If
End Sub

Aho
September 16th, 1999, 05:52 AM
Thanks For All repliers; it worked fine (for controls).

For Menu Commands I had to do more stuff.

Ahmed