INTERDIRE LA SAISIE DES CARACTERES DE VOTRE CHOIX

cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 - 22 sept. 2005 à 14:51
bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 - 22 sept. 2005 à 18:03
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/33897-interdire-la-saisie-des-caracteres-de-votre-choix

bouv Messages postés 1411 Date d'inscription mercredi 6 août 2003 Statut Membre Dernière intervention 3 mars 2019 1
22 sept. 2005 à 18:03
On peut aussi faire

Private Sub Form_KeyPress(KeyAscii As Integer)
Const AutorizedChars = "0123456789" & Chr$(25)
If Instr(AutorizedChars, Chr$(KeyAscii)) 0 then KeyAscii 0
End Sub

ou

Private Sub Form_KeyPress(KeyAscii As Integer)
Const RestrictedChars = "LJBDVLKUH" & Chr$(13) & Chr$(8)
If Not Instr(RestrictedChars, Chr$(KeyAscii)) 0 then KeyAscii 0
End Sub
Totoroyamada Messages postés 31 Date d'inscription jeudi 26 mai 2005 Statut Membre Dernière intervention 17 novembre 2005
22 sept. 2005 à 16:39
Ok, c'est juste que moi quand dans Visual Studio j'écris
Private Sub toto(tutu As Integer)
...
End Sub
il me corrige ça tout seul en
Private Sub toto(ByVal tutu As Integer)
...
End Sub
et j'ai beau vouloir enlever le "ByVal", il me le remet automatiquement... d'où la nécessité de préciser moi-même ByRef...
'fin bon disons qu'on avait tous les deux à moitié raison ;-)
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
22 sept. 2005 à 16:32
Et non TOTOROYAMADA, le ByRef, si on ne le mais pas dans la déclaration d'un sub, il est pris par défaut, par contre, c'est vrai qu'il faut enlever le As Integer, donc ca donne :
Private Sub Bad_Char(KeyAscii As Integer)
Select Case KeyAscii
Case 49, 50, 51, 52, 53, 54, 55, 56, 57, 58: KeyAscii = 0
End Select
End Sub
Private Sub Text1_Change()
Bad_Char (KeyAscii)
End Sub
Totoroyamada Messages postés 31 Date d'inscription jeudi 26 mai 2005 Statut Membre Dernière intervention 17 novembre 2005
22 sept. 2005 à 15:19
Je peux me tromper (parce que là tout de suite je peux pas tester), mais ton truc ne doit pas marcher DS...
Il faut soit faire
KeyAscii = Bad_Char(KeyAscii)
soit déclarer ta sub comme ça:
Sub Bad_Char(ByRef KeyAscii As Integer)
...
End Sub
cs_DARKSIDIOUS Messages postés 15814 Date d'inscription jeudi 8 août 2002 Statut Membre Dernière intervention 4 mars 2013 130
22 sept. 2005 à 14:51
Tu peux également utiliser une Sub, avec une utilisation encore plus simple :

1. Private Sub Form_KeyPress(KeyAscii As Integer)
2. Bad_Char(KeyAscii)
3. End Sub
4.
5. Sub Bad_Char(KeyAscii As Integer) As Integer
6. Select Case KeyAscii
7. Case 62, 60, 34, 63, 42, 58, 92, 46, 47, 124: KeyAscii = 0
8. End Select
9. End Sub
Rejoignez-nous