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
Private Sub txtsal_KeyPress(KeyAscii As Integer) 'Pour ne rentrer que des chiffres If InStr("0123456789" & Chr(8), Chr(KeyAscii)) = 0 Then Check = MsgBox("les caractères numériques sont acceptés !", vbExclamation + vbOKOnly, "Attention") KeyAscii = 0 End If End Sub
Option Explicit Private Const GWL_STYLE As Long = -16 Public Const ES_NUMBER As Long = &H2000& Private Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Sub RestrictTextboxToNumbers(txt As TextBox) SetWindowLong txt.hwnd, GWL_STYLE, GetWindowLong(txt.hwnd, GWL_STYLE) Or ES_NUMBER End Sub---
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionPrivate Sub txtsal_KeyPress(KeyAscii As Integer) If InStr("0123456789" & Chr(8) & Chr(13), Chr(KeyAscii)) = 0 Then Check = MsgBox("Seul les caractères numériques sont acceptés !", vbExclamation + vbOKOnly, "Attention") KeyAscii = 0 End If End Sub