[VB6] Comment obliger la saisie d'un nombre?

Résolu
js8bleu Messages postés 576 Date d'inscription   Statut Membre Dernière intervention   -  
 lwizti -
Bonjour,

je développe sous VB 6.0. J'ai un champ salaire (txtsal) et j'aimerai contrôler le fait que seule la saisie de chiffre peut être accepter dans ce champ. Voici mon code qui marche assez bien mais malheureusement même si je saisi un chiffre dans le champ, si j'appuie la touche entrée il m'affiche le message comme quoi ce n'est pas un nombre. Voici mon code :


Private Sub txtsal_KeyPress(KeyAscii As Integer)
    If KeyAscii <> 8 Then
        If Not IsNumeric(Chr(KeyAscii)) Then
            KeyAscii = 0
            MsgBox "Veuillez entrer un nombre SVP!", vbInformation
        End If
    End If
End Sub


Quelqu'un pourrait-il m'aider s'il vous plaît?

Merci d'avance.

Cordialement.
A voir également:

3 réponses

calibos
 
Bonjour,

Tu peux t'en sortir en verrouillant le textbox pour tout caractère invalide
Si tu veux autoriser uniquement les chiffres et le point décimal voila comment faire:
(Il faut ajouter les deux sub )

Private Sub txtsal_KeyPress(KeyAscii As Integer)
Dim a As Integer
   a = KeyAscii
   If a < 32 Then Exit Sub
   If (a < 48 Or a > 57) And a <> 46 Then
      txtsal.Locked = True
      'eventuellement afficher message
   End If
End Sub

Private Sub txtsal_KeyUp(KeyCode As Integer, Shift As Integer)
   txtsal.Locked = False
End Sub


A+.
0
js8bleu Messages postés 576 Date d'inscription   Statut Membre Dernière intervention   4
 
Bonjour et Merci calibos pour ton aide. J'ai pu trouver une solution à mon problème grâce à ce code :

    If KeyAscii = 13 Then

    'si on appuie sur la touche backspace et del
    ElseIf KeyAscii = 8 Or KeyAscii = 46 Then
        
	Traitement
    
    ElseIf KeyAscii <> 8 Then

        If Not IsNumeric(Chr(KeyAscii)) Then
            KeyAscii = 0
            MsgBox "Veuillez entrer un nombre SVP!", vbInformation
        End If
    
    End If


Merci et excellente soirée.

Cordialement.
0
lwizti
 
Private Sub Text1_KeyPress(KeyAscii As Integer)

If InStr("0123456789.," + Chr$(8), Chr$(KeyAscii)) = 0 Then
KeyAscii = 0
End If


End Sub
0