VALIDATION DE SAISIE POUR TEXTBOX

sonacha - 1 juin 2001 à 11:27
cs_megamario Messages postés 145 Date d'inscription lundi 15 juin 2009 Statut Membre Dernière intervention 14 février 2013 - 13 févr. 2010 à 20:04
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/1217-validation-de-saisie-pour-textbox

cs_megamario Messages postés 145 Date d'inscription lundi 15 juin 2009 Statut Membre Dernière intervention 14 février 2013
13 févr. 2010 à 20:04
Bonjour à vous, Pourriez vous indiquer comment mettre en application votre code car j'en aurai bien besoin et je bloque.

Je suis en VB.net 2008

Si je prend le code tel qu'elle j'ai des erreurs

J'ai nommer mon TxtBox en txtNumber J'ai declarrer des variables vbKeyDelete et vbKeyBack en integer

Lorsque je crée un évènement KeyDown et Keypress

J'ai :
Private Sub txtNumber_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumber.KeyDown
End Sub
et
Private Sub txtNumber_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber.KeyPress
End Sub

Si je le change rien ne se passe. Je suppose qu'il faut que j'utilise l'un des arguments passé dans la fonction, merci de m'eclairer
threadom Messages postés 101 Date d'inscription mardi 8 avril 2003 Statut Membre Dernière intervention 1 novembre 2007
22 mars 2008 à 17:12
4 ans après ...

Private Sub txtNumber_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyDelete) Then
If Len(txtNumber) < 2 Then
txtNumber.Text = "0"
KeyCode = 0
End If
End If
End Sub

Private Sub txtNumber_KeyPress(KeyAscii As Integer)
If (KeyAscii = vbKeyBack) Then
If Len(txtNumber) < 2 Then
txtNumber.Text = "0"
KeyAscii = 0
End If
Else
If Not Chr(KeyAscii) Like "[0123456789.]" Then
KeyAscii = 0
ElseIf Chr(KeyAscii) = "." Then
If InStr(txtNumber, ".") Then
KeyAscii = 0
End If
End If
End If
End Sub

Y'a surement plus propre et ect... mais bon. Personne je m'occupe des dates donc pas besoin de gérer le point.
Fabio972 Messages postés 62 Date d'inscription dimanche 22 octobre 2000 Statut Membre Dernière intervention 4 août 2004
27 janv. 2004 à 13:49
Petite participation (merci à toi, je rends à César...) :

Pour éviter la double saisie du caractère décimal (. ou ,) :

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 46, 44 ' 46 -> . | 44 -> ,
If InStr(Text1.Text, ",") > 0 Or InStr(Text1.Text, ".") > 0 Then KeyAscii = 0
Case Else
If Not Chr(KeyAscii) Like "[0-9,.]" And KeyAscii <> Asc(vbBack) Then KeyAscii = 0
End Select
End Sub
Fabio972 Messages postés 62 Date d'inscription dimanche 22 octobre 2000 Statut Membre Dernière intervention 4 août 2004
27 janv. 2004 à 13:30
Pas mal !!! Je ne connaissais pas !

Merci de l'astuce (du coup je remonte ta note car depuis le temps que je code en VB je l'avais jamais vu celle-là ;-)
segaroux Messages postés 2 Date d'inscription mardi 11 février 2003 Statut Membre Dernière intervention 12 avril 2003
12 avril 2003 à 11:52
pas mieux !
cs_iannick Messages postés 1 Date d'inscription vendredi 27 décembre 2002 Statut Membre Dernière intervention 8 janvier 2003
8 janv. 2003 à 03:05
Simple et efficace.
BELLIV Messages postés 5 Date d'inscription vendredi 21 décembre 2001 Statut Membre Dernière intervention 20 juillet 2004
26 déc. 2001 à 09:39
Pas grave il suffit d'autoriser les touches backspace et ret. arr. comme suit :
Private Sub Text1_KeyPress(KeyAscii As Integer)

If Not Chr(KeyAscii) Like "[0123456789.]" Or Keyascii<>toto Then KeyAscii = 0

où toto est le code du backspace par ex. , euh je me souviens plus le code :) (46 peut être)
ce code ne permettra pas à l'utilisateur un retour arriere pour effacer ce qu'il a saisi par erreur.
chose trés courante pendant la saisie.
Rejoignez-nous