OVH Cloud OVH Cloud

Mask dans TextBox

25 réponses
Avatar
Apitos
Bonjour,

Comment pour maitriser en general les mask dans les TextBox ?

dans mon cas ; Ref : ../../N=B0/Ann=E9e

Merci.

10 réponses

1 2 3
Avatar
Apitos
Bonsoir JB,

les touches Backspace, Gauche, Droite, Effacer, Insérer ne sont pas
gérées

et puis les entrées clavier se font tous par SHIFT+touche ou
ALT+touche

Pourquoi n'étend-on pas la saisie par toutes les touches ?

Merci.
Avatar
Apitos
Daniel wrote:
Change :
If Posit > 19 Then
valeurtextbox = Mid(.Value, 6, 2) & Mid(.Value, 9, 3) & _
Mid(.Value, 13, 3) & Right(.Value, 4)
MsgBox valeurtextbox
End If
en :
If Posit > 19 Then
[A1]= Mid(.Value, 6, 2) & Mid(.Value, 9, 3) & _
Mid(.Value, 13, 3) & Right(.Value, 4)
End If



Bonsoir Daniel,

Quand j'utilise la touche Backsapce pour une corriger une saisie, elle
efface tout même les barres obliques ...

Avatar
JB
Les flèches de déplacement droite et gauche sont gérées ainsi que
les chiffres du pavé numérique.
Sur l'exemple, il n'est pas nécessaire de gérer la touche
suppression.

http://cjoint.com/?jruEu0OQVb

Dim p
Dim masque
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
'MsgBox KeyCode
If KeyCode = 8 Or KeyCode = 37 Then
KeyCode = 0
If p > 0 Then p = p - 1
If p = 2 Or p = 5 Then p = p - 1
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
If p = 2 Or p = 5 Then p = p + 1
End If
If KeyCode = 46 Then KeyCode = 0 ' touche suppression
If Mid(masque, p + 1, 1) = "." Then
If Not (KeyCode >= 48 And KeyCode <= 58 Or KeyCode >= 96 And
KeyCode <= 106) Then KeyCode = 0
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "../../.."
TextBox1 = masque
p = 0
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
If p = 2 Then p = 3
If p = 5 Then p = 6
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(Me.TextBox1) Then
Cancel = True
End If
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub


JB

http://cjoint.com/?jruEu0OQVb



Bonsoir JB,

les touches Backspace, Gauche, Droite, Effacer, Insérer ne sont pas
gérées

et puis les entrées clavier se font tous par SHIFT+touche ou
ALT+touche

Pourquoi n'étend-on pas la saisie par toutes les touches ?

Merci.


Avatar
Daniel
"Quand j'utilise la touche Backsapce pour une corriger une saisie, elle
efface tout même les barres obliques ...
---
Bonsoir.
C'est ce que j'écrivais :
C'est seulement, j'en suis conscient, une ébauche de solution. Reste à gérer
le plus difficile : les retours arrière de la saisie, les copier / Coller
etc.
Daniel
Avatar
Apitos

Les flèches de déplacement droite et gauche sont gérées ainsi que
les chiffres du pavé numérique.
Sur l'exemple, il n'est pas nécessaire de gérer la touche
suppression.

http://cjoint.com/?jruEu0OQVb

Dim p
Dim masque
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
'MsgBox KeyCode
If KeyCode = 8 Or KeyCode = 37 Then
KeyCode = 0
If p > 0 Then p = p - 1
If p = 2 Or p = 5 Then p = p - 1
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
If p = 2 Or p = 5 Then p = p + 1
End If
If KeyCode = 46 Then KeyCode = 0 ' touche suppression
If Mid(masque, p + 1, 1) = "." Then
If Not (KeyCode >= 48 And KeyCode <= 58 Or KeyCode >= 96 And
KeyCode <= 106) Then KeyCode = 0
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "../../.."
TextBox1 = masque
p = 0
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
If p = 2 Then p = 3
If p = 5 Then p = 6
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(Me.TextBox1) Then
Cancel = True
End If
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub


JB

http://cjoint.com/?jruEu0OQVb



Ton exemple :

Masque de saisie pour date (déplacement avec flèches)

me convient mieux.

mais la saisie des lettres n'est pas acceptée, pourquoi ? et comment y
parvenir ?

J'ai ajouter cette ligne :

TextBox1.MaxLenght = 10

pour limterla saisie dans le TextBox1.

Encore grand merci pour tes meuilleurs exemples.

JB : T'as pas un site personnel ?

Avatar
Apitos

"Quand j'utilise la touche Backsapce pour une corriger une saisie, elle
efface tout même les barres obliques ...
---
Bonsoir.
C'est ce que j'écrivais :
C'est seulement, j'en suis conscient, une ébauche de solution. Reste à gérer
le plus difficile : les retours arrière de la saisie, les copier / Coll er
etc.
Daniel


Tu as raison Daniel.

Jai du me precipiter dans la réponse.

;)

Avatar
JB
La touche Suppression(code 46) est gérée et les lettres acceptées.

http://cjoint.com/?jseOyQeWV6

Dim p
Dim masque
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
If KeyCode = 8 Or KeyCode = 37 Then ' flèche gauche et backspace
KeyCode = 0
If p > 0 Then p = p - 1
If p = 2 Or p = 5 Then p = p - 1
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
If p = 2 Or p = 5 Then p = p + 1
End If
If Mid(masque, p + 1, 1) = "." Then
If KeyCode = 46 Then ' touche suppression
Me.TextBox1 = Left(Me.TextBox1, p) & "." & Mid(Me.TextBox1, p +
2)
End If
If Not (KeyCode >= 48 And KeyCode <= 58 Or _
KeyCode >= 96 And KeyCode <= 106 Or _
KeyCode >= 65 And KeyCode <= 65 + 26) Then KeyCode = 0 '
Chiffres & lettes
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "../../.."
TextBox1 = masque
p = 0
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
If p = 2 Then p = 3
If p = 5 Then p = 6
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(Me.TextBox1) Then
Cancel = True
End If
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub

JB

http://www.excelabo.net




Les flèches de déplacement droite et gauche sont gérées ainsi q ue
les chiffres du pavé numérique.
Sur l'exemple, il n'est pas nécessaire de gérer la touche
suppression.

http://cjoint.com/?jruEu0OQVb

Dim p
Dim masque
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
'MsgBox KeyCode
If KeyCode = 8 Or KeyCode = 37 Then
KeyCode = 0
If p > 0 Then p = p - 1
If p = 2 Or p = 5 Then p = p - 1
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
If p = 2 Or p = 5 Then p = p + 1
End If
If KeyCode = 46 Then KeyCode = 0 ' touche suppression
If Mid(masque, p + 1, 1) = "." Then
If Not (KeyCode >= 48 And KeyCode <= 58 Or KeyCode >= 96 And
KeyCode <= 106) Then KeyCode = 0
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "../../.."
TextBox1 = masque
p = 0
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
If p = 2 Then p = 3
If p = 5 Then p = 6
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(Me.TextBox1) Then
Cancel = True
End If
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub


JB

http://cjoint.com/?jruEu0OQVb



Ton exemple :

Masque de saisie pour date (déplacement avec flèches)

me convient mieux.

mais la saisie des lettres n'est pas acceptée, pourquoi ? et comment y
parvenir ?

J'ai ajouter cette ligne :

TextBox1.MaxLenght = 10

pour limterla saisie dans le TextBox1.

Encore grand merci pour tes meuilleurs exemples.

JB : T'as pas un site personnel ?



Avatar
JB
Avec cette version:
-On distingue les chiffres(.) et les lettres (-)
-Les sauts sont calculés automatiquement

http://cjoint.com/?jsfJctTWG1

Dim p
Dim masque
Dim sauts(10), ns

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
If KeyCode = 8 Or KeyCode = 37 Then ' flèche gauche et backspace
KeyCode = 0
If p > 0 Then p = p - 1
For i = 1 To ns
If p = sauts(i) Then p = p - 1
Next i
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
For i = 1 To ns
If p = sauts(i) Then p = p + 1
Next i
End If
'-- chiffres
If Mid(masque, p + 1, 1) = "." Then
If KeyCode = 46 Then ' touche suppression
Me.TextBox1 = Left(Me.TextBox1, p) & "." & Mid(Me.TextBox1, p +
2)
End If
If Not (KeyCode >= 48 And KeyCode <= 58 Or _
KeyCode >= 96 And KeyCode <= 106) Then KeyCode = 0 ' Chiffres
End If
'-- lettres
If Mid(masque, p + 1, 1) = "-" Then
If KeyCode = 46 Then ' touche suppression
Me.TextBox1 = Left(Me.TextBox1, p) & "-" & Mid(Me.TextBox1, p +
2)
End If
If Not (KeyCode >= 65 And KeyCode <= 65 + 26) Then KeyCode = 0 '
lettres
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "../../--"
ns = 0
For i = 1 To Len(masque)
If Mid(masque, i, 1) <> "." And Mid(masque, i, 1) <> "-" Then
ns = ns + 1
sauts(ns) = i - 1
End If
Next i
TextBox1 = masque
p = 0
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
For i = 1 To ns
If p = sauts(i) Then p = p + 1
Next i
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub

JB






Les flèches de déplacement droite et gauche sont gérées ainsi q ue
les chiffres du pavé numérique.
Sur l'exemple, il n'est pas nécessaire de gérer la touche
suppression.

http://cjoint.com/?jruEu0OQVb

Dim p
Dim masque
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
'MsgBox KeyCode
If KeyCode = 8 Or KeyCode = 37 Then
KeyCode = 0
If p > 0 Then p = p - 1
If p = 2 Or p = 5 Then p = p - 1
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
If p = 2 Or p = 5 Then p = p + 1
End If
If KeyCode = 46 Then KeyCode = 0 ' touche suppression
If Mid(masque, p + 1, 1) = "." Then
If Not (KeyCode >= 48 And KeyCode <= 58 Or KeyCode >= 96 And
KeyCode <= 106) Then KeyCode = 0
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "../../.."
TextBox1 = masque
p = 0
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
If p = 2 Then p = 3
If p = 5 Then p = 6
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not IsDate(Me.TextBox1) Then
Cancel = True
End If
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub


JB

http://cjoint.com/?jruEu0OQVb



Ton exemple :

Masque de saisie pour date (déplacement avec flèches)

me convient mieux.

mais la saisie des lettres n'est pas acceptée, pourquoi ? et comment y
parvenir ?

J'ai ajouter cette ligne :

TextBox1.MaxLenght = 10

pour limterla saisie dans le TextBox1.

Encore grand merci pour tes meuilleurs exemples.

JB : T'as pas un site personnel ?



Avatar
Apitos
Merci JB.

Je vais le testé ...
Avatar
JB
Essai avec :

Ref --- Date ../../..

http://cjoint.com/?jsh6j78dIZ


Dim p
Dim masque
Dim sauts(20), ns
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer)
If KeyCode = 8 Or KeyCode = 37 Then ' flèche gauche et backspace
KeyCode = 0
If p > 0 Then p = p - 1
For i = ns To 1 Step -1
If p = sauts(i) Then p = p - 1
Next i
If p < 0 Then
p = 0
For i = 1 To ns
If p = sauts(i) Then p = p + 1
Next i
End If
End If
If KeyCode = 39 Then ' flèche droite
KeyCode = 0
p = p + 1
For i = 1 To ns
If p = sauts(i) Then p = p + 1
Next i
End If
If p >= Len(masque) Then KeyCode = 0: Exit Sub
'-- chiffres
If Mid(masque, p + 1, 1) = "." Then
If KeyCode = 46 Then ' touche suppression
Me.TextBox1 = Left(Me.TextBox1, p) & "." & Mid(Me.TextBox1, p +
2)
End If
If Not (KeyCode >= 48 And KeyCode <= 58 Or _
KeyCode >= 96 And KeyCode <= 106) Then KeyCode = 0 ' Chiffres
End If
'-- lettres
If Mid(masque, p + 1, 1) = "-" Then
If KeyCode = 46 Then ' touche suppression
Me.TextBox1 = Left(Me.TextBox1, p) & "-" & Mid(Me.TextBox1, p +
2)
End If
If Not (KeyCode >= 65 And KeyCode <= 65 + 26) Then KeyCode = 0 '
lettres
End If
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub UserForm_Initialize()
masque = "Ref --- Date ../../.."
ns = 0
For i = 1 To Len(masque)
If Mid(masque, i, 1) <> "." And Mid(masque, i, 1) <> "-" Then
ns = ns + 1
sauts(ns) = i - 1
End If
Next i
TextBox1 = masque
p = 0
For i = 1 To ns
If p = sauts(i) Then p = p + 1
Next i
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_Change()
p = p + 1
For i = 1 To ns
If p = sauts(i) Then p = p + 1
Next i
TextBox1.SelStart = p
TextBox1.SelLength = 1
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As
Integer, ByVal X As Single, ByVal Y As Single)
p = Me.TextBox1.SelStart
End Sub

JB



Merci JB.

Je vais le testé ...


1 2 3