Soyez le premier à donner votre avis sur cette source.
Snippet vu 4 635 fois - Téléchargée 29 fois
' Il faut sur une form: ' Un bouton (Command1) ' Un TextBox nommé(s) txtNombre_nr ' Des autres TextBoxe, si tu le souhaites... Private Sub Command1_Click() If Not IsValidForm Then MsgBox ("Form validé avec succès !") End Sub Private Sub txtNombre_nr_LostFocus() If Not IsNumeric(txtNombre_nr) Then NotNumeric (Me.Controls("txtNombre_nr")) End Sub Private Function IsValidForm() As Boolean Dim C As Control IsValidForm = True For Each C In Form1.Controls() If Right$(C.Name, 3) = "_nr" Then If TypeOf C Is TextBox Then If Not IsNumeric(C.Text) Then NotNumeric (Me.Controls(C.Name)) IsFalidForm = False Exit Function End If End If End If Next End Function Private Sub NotNumeric(TB As TextBox) MsgBox ("Veuillez entrer un nombre !") TB.SetFocus TB.SelStart = 0 TB.SelLength = Len(TB) End Sub
22 mars 2004 à 08:37
Ensuite tu rajoutes la condition :
Si monTextBox.tag = 1 then...je bloque les touches
14 mars 2004 à 13:26
14 mars 2004 à 12:47
le plus souvent je bloque les touches comme Renfield.
par contre, je trouve plus simple d'utiliser les KeyPress avec le paramètre KeyASCII, plutot que d'utiliser une API.
nono
13 mars 2004 à 13:35
13 mars 2004 à 07:14
Un textBox ne se nomme pas forcément "txt_qqChose" (je les nomme comme ca aussi, remarque...)
Je laisse le suffixe "_nr"...
Private Sub Command1_Click()
Dim C As Control
For Each C In Me.Controls
If TypeOf C Is TextBox Then
If Right$(C.Name, 3) = "_nr" And
If Not IsNumeric(C.Text) Then
NotNumeric (Me.Controls(C.Name))
Exit Sub
End If
End If
Next C
MsgBox ("Form validé avec succès !")
End Sub
Private Sub txtNombre_nr_LostFocus()
If Not IsNumeric(txtNombre_nr) Then NotNumeric (txtNombre_nr)
End Sub
------------------------------------------
Une autre solution serait de bloquer les touches :
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000&
Public Sub SetNumber(NumberText As TextBox, Optional Flag As Boolean = True)
'# On récupère le style du textbox
Dim Style As Long: Style = GetWindowLong(NumberText.hwnd, GWL_STYLE)
'# Si on souhaite activer le 'mode numérique'
If Flag Then
'# On ajoutes la constante ES_NUMBER au style...
Style = Style Or ES_NUMBER
Else
'# Sinon, on l'en enlève.
Style = Style Xor ES_NUMBER
End If
'# On replace le style dans le testbox
Style = SetWindowLong(NumberText.hwnd, GWL_STYLE, Style)
End Sub
Private Sub Form_Load()
Dim Ctl As Control
'# On regarde chaque controle de la form
For Each Ctl In Me.Controls
'# Si l'objet considéré est un TextBox dont le nom finit par '_nr' (ou _Nr / _NR / _nR)...
If (TypeOf Ctl Is TextBox) And (0 = StrComp(Right$(Ctl.Name, 3), "_nr", vbTextCompare)) Then
'# On active le 'mode numérique' pour ce controle...
SetNumber Ctl
End If
Next Ctl
End Sub
---
Attention, on peut contourner ce code par un simple Ctrl+C Ctrl+V
------------------------
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.