NHenry
Messages postés15102Date d'inscriptionvendredi 14 mars 2003StatutModérateurDernière intervention27 mars 2024159 24 janv. 2012 à 19:16
Bonjour,
Normal que ton code arrive à plus, tu n'a pas déclaré tes variables, du coup, elles sont Variant, donc pour plus, tu passes en Double, tu perds pas mal de précision par contre.
Mets TOUJOURS Option Explicit en haut de tout tes modules de code en VB6 et VBA.
loulou27200
Messages postés3Date d'inscriptionmercredi 6 mai 2009StatutMembreDernière intervention 2 août 2011 24 janv. 2012 à 15:00
Effectivement ton code est plus court
Mais je débute dans la programmation que je connais pas toutes les fonctions, ton code bloc à 2147483648 alors que le mien semble pouvoir dépasser cette valeur.
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 24 janv. 2012 à 13:33
Presse papier toujours une faille, mais code plus court :
Option Explicit
Private Const ES_NUMBER As Long = &H2000&
Private Const GWL_STYLE As Long = -16
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
Private Sub Form_Load()
SetWindowLong Text1.hwnd, GWL_STYLE, GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
End Sub
Private Sub Text1_Change()
Label1.Caption = DecToHexa2(Text1.Text)
End Sub
Public Function DecToHexa2(ByRef StringNb As String) As String
If Val(StringNb) Then
DecToHexa2 = Hex$(StringNb)
Else
DecToHexa2 = "0"
End If
End Function
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 24 janv. 2012 à 13:10
oui, mais on ne controle pas la saisie a la volée, c'est souvent vecteur d'inconfort...
rien n'empeche de saisir des caractères non autorisés via le presse papier.
loulou27200
Messages postés3Date d'inscriptionmercredi 6 mai 2009StatutMembreDernière intervention 2 août 2011 24 janv. 2012 à 12:09
Pourquoi ce n'est pas bon! le control text est destiné à recevoir des chiffres et non du text, je prévois que l'utilisateur se trompe!!
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 24 janv. 2012 à 11:18
Es-tu faché avec la fonction Hex$ ?
Pas bon, ce genre de choses :
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = 44 Or KeyAscii = 46) Then
KeyAscii = 0 'on annule la pression sur la touche
End If
End Sub
pas hermetique aux fautes de saisie (presse papier, etc)
24 janv. 2012 à 19:16
Normal que ton code arrive à plus, tu n'a pas déclaré tes variables, du coup, elles sont Variant, donc pour plus, tu passes en Double, tu perds pas mal de précision par contre.
Mets TOUJOURS Option Explicit en haut de tout tes modules de code en VB6 et VBA.
24 janv. 2012 à 15:00
Mais je débute dans la programmation que je connais pas toutes les fonctions, ton code bloc à 2147483648 alors que le mien semble pouvoir dépasser cette valeur.
24 janv. 2012 à 13:33
Option Explicit
Private Const ES_NUMBER As Long = &H2000&
Private Const GWL_STYLE As Long = -16
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
Private Sub Form_Load()
SetWindowLong Text1.hwnd, GWL_STYLE, GetWindowLong(Text1.hwnd, GWL_STYLE) Or ES_NUMBER
End Sub
Private Sub Text1_Change()
Label1.Caption = DecToHexa2(Text1.Text)
End Sub
Public Function DecToHexa2(ByRef StringNb As String) As String
If Val(StringNb) Then
DecToHexa2 = Hex$(StringNb)
Else
DecToHexa2 = "0"
End If
End Function
24 janv. 2012 à 13:10
rien n'empeche de saisir des caractères non autorisés via le presse papier.
voir le flag ES_NUMBER :
http://www.codyx.org/snippet_textbox-numerique-seulement_53.aspx
24 janv. 2012 à 12:09
24 janv. 2012 à 11:18
Pas bon, ce genre de choses :
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = 44 Or KeyAscii = 46) Then
KeyAscii = 0 'on annule la pression sur la touche
End If
End Sub
pas hermetique aux fautes de saisie (presse papier, etc)