|
-
Jun 24th, 2002, 03:15 AM
#1
Thread Starter
Addicted Member
LostFocus Event Problem
Hi,
I have a form with 2 textboxes and a command button.
There are some codes written on the LostFocus event of
textbox1. The codes are as follow :
Private Sub textbox1_LostFocus()
If Not IsNumeric(Trim(textbox1.Text) Then
MsgBox "Please enter a numeric value !", vbOKOnly +
vbCritical, "ERROR"
End If
End Sub
To test on the code, I key in some characters in textbox1 & click
the command button. The message as shown above was displayed. But the codes of the command button was not executed. Does anyone know where has gone wrong ? Thanks !
Dolly
-
Jun 24th, 2002, 03:17 AM
#2
If you press F8, you can step thru the code and see the sequence in which the code is executed. My guess is that when u press the command button, two things happen: text1 loses focus, and command1 is clicked. text1 probably gets executed first.
So you should step thru, and observe flow of execution.
-
Jun 24th, 2002, 03:47 AM
#3
Thread Starter
Addicted Member
Yes, textbox1 lostfocus after clicking the command button.
But I expect the codes in the command button to execute
as well, after I click the OK button of the MsgBox.
The lostfocus codes has to be there. What can I do to
ensure that codes in the command button will eventually be
executed ?
Thanks
-
Jun 24th, 2002, 03:53 AM
#4
It's due to the presence of the check on the textbox1 value that the command button won't execute.
If you enter a numeric value, the code WILL execute.
is this what you wanted, or do you want the command button's code to be executed no matter what?
-
Jun 24th, 2002, 09:17 AM
#5
Thread Starter
Addicted Member
The command button acts as an exit button, that is, it will
close the form even if the user key in a non-numeric value in
textbox1. If user have to key in a numeric value in the textbox
before clicking on the exit button, he will get frustrated.
-
Jun 24th, 2002, 09:24 AM
#6
Don't use the lostfocus event for it.
Use the change event instead.
The lostfocus event isn't such a good option.
-
Jun 24th, 2002, 09:29 AM
#7
PowerPoster
Well
Could you use validate?
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
End Sub
This fires before the lostfocus event and allows you to cancel if necessary...
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Jun 24th, 2002, 10:03 AM
#8
Frenzied Member
You should NOT use the LostFocus event for validating controls. Since VB6 a Validate event has appeared which should be used for this:
Code:
Private Sub textbox1_Validate(Cancel as boolean)
If Not IsNumeric(Trim(textbox1.Text) Then
MsgBox "Please enter a numeric value !", vbOKOnly +
vbCritical, "ERROR"
Cancel=true
End If
End Sub
Then, on the button, set the CausesValidation property to FALSE and then the validate event for the control losing focus when you press the button will not be fired. Useful for CANCEL buttons etc.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 24th, 2002, 10:51 AM
#9
Thread Starter
Addicted Member
Hi all,
Thanks for your replies.
By the way Buzby, I need to use the lostfocus event not only
for validating the controls, I also need to do some calculation
& display the result in textbox2. The result has to be displayed
automatically once some value is key in textbox1.
Now, if user key in some non-numeric value in textbox but later
wish to close the form without having to view the result in textbox2, is it possible to do that ?
I was thinking of validating the controls using the method you have just mention while doing the calculation & displaying the result in textbox2 using the lostfocus event of textbox1. Textbox2 is disabled & user can't key in any value.
Is setting the Cancel variable of the validate event to true stop the execution of the codes in lostfocus event when the command button is clicked ? Thanks!
-
Jun 24th, 2002, 10:57 AM
#10
Frenzied Member
1. The LostFocus event should be used for code that should run EVERY TIME the control loses focus - ie, updating other controls etc.
2. The Validate event should be used for validation - and as I said before, you can select whether this event is run when a button is clicked - if you set Cancel=TRUE in a Validate event the focus remains with the control. Ie, the user cannot leave the control until the Validate event is happy!
3. Normally, you leave CausesValidation = TRUE on the OK, Save, etc buttons, and switch it to FALSE for the Cancel, Exit etc buttons. That way if a user wants to cancel the form/program they are not forced to get the current control validated first.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 24th, 2002, 12:15 PM
#11
If you would like to limit the textbox to numbers (and optionally specify such things as if it can or cannot be negative or if it can have decimals) you can download a control that I wrote. You can find it in my post inthis thread
-
Jun 24th, 2002, 11:44 PM
#12
Thread Starter
Addicted Member
I have tried to set the CausesValidation property of my exit button to false but the validate event of textbox1 still executed when I key in some non-numeric value to textbox1 and then click
the command button (cmdExit). I thought that after setting CausesValidation property to false, the validate event should not be executed, did I miss out anything in my code as shown below?
Private Sub textbox1_Validate(Cancel as boolean)
If Not IsNumeric(Trim(textbox1.Text) Then
MsgBox "Please enter a numeric value !", vbOKOnly +
vbCritical, "ERROR"
Cancel=true
End If
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
-
Jun 25th, 2002, 03:28 AM
#13
Frenzied Member
Well when I use it it works as I would expect - are you sure you have got rid of the code in the LostFocus event of the textbox.
Set up a simple form with a textbox and a button.
In the textbox's validate event put:
VB Code:
Private Sub Text1_Validate(Cancel As Boolean)
Debug.Print "Validate"
End Sub
Then run the program with the CausesValidation event switched to FALSE on the button - if the focus is in the textbox and you click on the button you should NOT see "Validate" turn up in the Immediate window - ie the Validate event is not being run.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 25th, 2002, 08:47 AM
#14
Thread Starter
Addicted Member
Thanks for all your help.
I have found the "bug" in my program.
The CausesValidation can't work because at certain part of the code, I have accidentally set the Enabled property of the command button to false.
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
|