fabiin
-
18 mai 2001 à 17:50
Galactus13
Messages postés335Date d'inscriptionlundi 29 septembre 2008StatutMembreDernière intervention16 août 2023
-
10 mars 2009 à 01:28
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.
-----
Private Sub txtBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 46, 44 ' 46 -> . | 44 -> ,
If InStr(txtBox.Text, ",") > 0 Or InStr(txtBox.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
-----
cs_GMI
Messages postés3Date d'inscriptionmercredi 20 décembre 2000StatutMembreDernière intervention19 avril 2002 15 avril 2002 à 01:33
ikariShinji
Messages postés18Date d'inscriptionmercredi 13 mars 2002StatutMembreDernière intervention 2 juin 2005 13 mars 2002 à 13:30
Merci à toi Néo.balastik pour ce code bien pratique !
Toutefois, ton code empêche du coup la saisie d'un nombre à virgule, voici une solution simple pour régler le problème :
Dim SvgVirgule As Boolean
Private Sub Form_Load()
SetNumber txtBox, True
SvgVirgule = False
End Sub
Private Sub txtBox_KeyPress(KeyAscii As Integer)
' on empêche de saisir une virgule dans un TextBox vide
If txtBox.Text = "" Then
SvgVirgule = False
Exit Sub
End If
' si on supprime la virgule, on doit pouvoir en remettre une
If KeyAscii = 8 Then
If Right(txtBox.Text, 1) "," Then SvgVirgule False
End If
' on vérifie qu'il n'y a pas déjà une virgule
If KeyAscii 46 Or KeyAscii 44 Then
If SvgVirgule = True Then Exit Sub
End If
' si c un point, on le convertit en virgule
If KeyAscii 46 Then KeyAscii 44
' si c une virgule, on la met
If KeyAscii = 44 Then
SetNumber txtBox, False
SvgVirgule = True
End If
End Sub
Private Sub txtBox_KeyUp(KeyCode As Integer, Shift As Integer)
SetNumber txtBox, True
End Sub
( Vous trouverez bientôt une petite application de ce code - convertisseur Euro/Francs - sur mon site )
fou volant13
Messages postés55Date d'inscriptionvendredi 29 juin 2001StatutMembreDernière intervention 2 février 2002 19 déc. 2001 à 16:47
sinon ya plus bourrin mais tout autant efficace:
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 96 Or KeyCode > 105 And KeyCode <> vbKeyDecimal Then
Text1 = Mid(Text1, 1, Len(Text1) - 1)
Text1.SelStart = Len(Text1)
End If
End Sub
C bof, mais pour ceux qu'on pas envie de se casser le cul ca peut marcher.
REMARQUE: ca marche pas avec les copiers collés
10 mars 2009 à 01:28
27 janv. 2004 à 13:52
-----
Private Sub txtBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 46, 44 ' 46 -> . | 44 -> ,
If InStr(txtBox.Text, ",") > 0 Or InStr(txtBox.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
-----
15 avril 2002 à 01:33
13 mars 2002 à 13:30
Toutefois, ton code empêche du coup la saisie d'un nombre à virgule, voici une solution simple pour régler le problème :
Dim SvgVirgule As Boolean
Private Sub Form_Load()
SetNumber txtBox, True
SvgVirgule = False
End Sub
Private Sub txtBox_KeyPress(KeyAscii As Integer)
' on empêche de saisir une virgule dans un TextBox vide
If txtBox.Text = "" Then
SvgVirgule = False
Exit Sub
End If
' si on supprime la virgule, on doit pouvoir en remettre une
If KeyAscii = 8 Then
If Right(txtBox.Text, 1) "," Then SvgVirgule False
End If
' on vérifie qu'il n'y a pas déjà une virgule
If KeyAscii 46 Or KeyAscii 44 Then
If SvgVirgule = True Then Exit Sub
End If
' si c un point, on le convertit en virgule
If KeyAscii 46 Then KeyAscii 44
' si c une virgule, on la met
If KeyAscii = 44 Then
SetNumber txtBox, False
SvgVirgule = True
End If
End Sub
Private Sub txtBox_KeyUp(KeyCode As Integer, Shift As Integer)
SetNumber txtBox, True
End Sub
( Vous trouverez bientôt une petite application de ce code - convertisseur Euro/Francs - sur mon site )
19 déc. 2001 à 16:47
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode < 96 Or KeyCode > 105 And KeyCode <> vbKeyDecimal Then
Text1 = Mid(Text1, 1, Len(Text1) - 1)
Text1.SelStart = Len(Text1)
End If
End Sub
C bof, mais pour ceux qu'on pas envie de se casser le cul ca peut marcher.
REMARQUE: ca marche pas avec les copiers collés
21 mai 2001 à 08:28
Pas vraiment convaincant comme façon de faire. J'ai essayé, et l'effet escompté est incertain.
Exemple: Si l'on tape une valeur numérique puis que l'on tape 2x successivement sur une touche alphabétique, la zone se remet à ZERO !!!!
Qu'en dites-vous ?
18 mai 2001 à 19:29
18 mai 2001 à 17:50
il suffit de transformer ce ki y'a dans le textbox en valeur numérique :
Dans la précédure Change du Text1, mettre
Val(Text1)
@+
fabs