Results 1 to 14 of 14

Thread: LostFocus Event Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    149

    Question 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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    149
    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

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    149

    Unhappy

    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.

  6. #6
    Swatty
    Guest
    Don't use the lostfocus event for it.

    Use the change event instead.

    The lostfocus event isn't such a good option.

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Could you use validate?

    VB Code:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2.  
    3. 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....

  8. #8
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    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."

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    149
    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!

  10. #10
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    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."

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    149
    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

  13. #13
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    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:
    1. Private Sub Text1_Validate(Cancel As Boolean)
    2. Debug.Print "Validate"
    3. 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."

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    149
    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
  •  



Click Here to Expand Forum to Full Width