Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 2021
-
13 mars 2004 à 07:14
jmlucienvb
Messages postés129Date d'inscriptionmercredi 24 septembre 2003StatutMembreDernière intervention12 février 2009
-
22 mars 2004 à 08:37
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.
jmlucienvb
Messages postés129Date d'inscriptionmercredi 24 septembre 2003StatutMembreDernière intervention12 février 2009 22 mars 2004 à 08:37
Si tu veux que cela ne concerne que quelques textbox, tu utilises la propriété tag en y mettant une valeur particulière par exemple 1
Ensuite tu rajoutes la condition :
Si monTextBox.tag = 1 then...je bloque les touches
cs_seyev
Messages postés93Date d'inscriptionsamedi 9 juin 2001StatutMembreDernière intervention 6 mai 20051 14 mars 2004 à 13:26
Oui, le problème, c'est pour gérer les touches comme retour arrière, entrée etc... De toutes facons, il y a plein de techniques pour résoudre ce genre de problèmess...
cs_nono1135
Messages postés52Date d'inscriptionjeudi 5 septembre 2002StatutMembreDernière intervention 2 mars 2006 14 mars 2004 à 12:47
Salut
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
cs_seyev
Messages postés93Date d'inscriptionsamedi 9 juin 2001StatutMembreDernière intervention 6 mai 20051 13 mars 2004 à 13:35
Salut, J'ai voulu faire If TypeOf C Is TextBox Then mais je ne savais pas si c'était possible et je n'ai pas testé... j'update la source !
Renfield
Messages postés17287Date d'inscriptionmercredi 2 janvier 2002StatutModérateurDernière intervention27 septembre 202174 13 mars 2004 à 07:14
Code quelques peu bancal, je dois dire....
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
------------------------
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
------------------------