Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Validation VB 5.0
Message
 
To
20/06/2001 18:30:27
General information
Forum:
Visual Basic
Category:
Other
Miscellaneous
Thread ID:
00521712
Message ID:
00521721
Views:
16
>Hello Guys
>
>What I am trying to do is some validation after the user presses the enter key on a text box. I tried putting the validation routine in the Lost Focus
>of the textbox. What happens next is very frustrating, the next textbox
>receives focus automatically The code will display a message in a message box that the contents of the textbox is invalid and then set focus back to the original textbox Now the lostfocus event of the next textbox is fired and now I am stuck in a loop.
>
>In the Keypressed event of he form (with KeyPreview = True), I check to see if the return key has been pressed then do a Sendkey {TAB} so that when the user presses the enter key on the textbox, focus will be set to the next textbox.
>
>I tried doing the validation in the Keypressed event of the TextBox
>and that seems to work just fine, but I cannot trap for when the user presses
>the Tab key to move to the next textbox.
>
>Is there some way that I can trap for the TAB key being pressed within the form
>or can anyone out there suggest a different approach???

About the loop, you need to keep a boolean value that tells if you are in the process of changing focus. See this short example:
Option Explicit

Private mblnValidation As Boolean

Private Sub cmdCancel_Click()
    Unload Me
End Sub

Private Sub cmdOK_Click()
    MsgBox "Sortie de la forme accepter !!!"
    Unload Me
End Sub

Private Sub txtNom_LostFocus()
    If Not mblnValidation And _
       Screen.ActiveControl.Name <> "cmdCancel" _
    Then
        If txtNom.Text = "" Then
            MsgBox "Le champs <<Nom>> est obligatoire", vbExclamation + vbOKOnly, "Erreur"
            mblnValidation = True
            txtNom.SetFocus
            DoEvents
            mblnValidation = False
        End If
    End If
End Sub

Private Sub txtPrenom_LostFocus()
    If Not mblnValidation And _
       Screen.ActiveControl.Name <> "cmdCancel" _
    Then
        If txtPrenom.Text = "" Then
            MsgBox "Le champs <<Prénom>> est obligatoire", vbExclamation + vbOKOnly, "Erreur"
            mblnValidation = True
            txtPrenom.SetFocus
            DoEvents
            mblnValidation = False
        End If
    End If
End Sub
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Previous
Reply
Map
View

Click here to load this message in the networking platform