1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| Const char_permis = "0123456789,"
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 46 Then TextBox1.Tag = "": TextBox1.Value = "0,00 " ' on vide le textbox et!!! son tag avec la touche suppr
If TextBox1.Tag <> "" Then 'si le tag est pas vide
If KeyCode = 8 Then TextBox1.Tag = Mid(TextBox1.Tag, 1, Len(TextBox1.Tag) - 1) 'on enelve le dernier chiffre dans le tag
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 46 Then KeyAscii = 44 ' on remplace le point par la virgule
If Chr(KeyAscii) = "," And TextBox1.Tag Like "*,*" Then KeyAscii = 0: Exit Sub 'si le caracteres tapé ou détourné est le separateur et que le textbox l'a deja on l'annule
If char_permis Like "*" & Chr(KeyAscii) & "*" Then ' si le caractere est permis
If InStr(TextBox1.Tag, ",") > 0 Then 'si il y a deja la virgule
If Len(Split(TextBox1.Tag, ",")(1)) >= 2 Then KeyAscii = 0 ' et que il y a deja 2 chiffres apres la virgule on annule la touche
End If
TextBox1.Tag = TextBox1.Tag & Chr$(KeyAscii) ' on met le caractere dans le tag
KeyAscii = 0 ' on annule bien evidement le keyascii car en vrai on ecrit pas dans le textbox du moins pas dans cet evenement
End If
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'voila c'est ici que l'on injecte le texte du tag dans le textbox
If TextBox1.Tag <> "" Then 'si le tag n'est pas vide
TextBox1.Value = Replace(Format(CDbl(TextBox1.Tag), "0.00 "), ",", ".") 'on injecte la valeur du tag formaté dans son .value avec le point comme separateur
'TextBox1.Value = Format(CDbl(TextBox1.Tag), "0.00 ") 'on injecte la valeur du tag formaté dans son .value avec la virgule comme separateur
TextBox1.SelStart = Len(TextBox1.Tag) ' on place le carré(clignotant)
End If
End Sub |