OVH Cloud OVH Cloud

Actions de format sur plusieurs TextBox

4 réponses
Avatar
Jacques
Salut a toutes et tous,

Voila j'ai trouvé ce code sur le forum (Et j'en remercie l'auteur), qui sert
a mettre le format téléphone dans un TextBox.

Dim Arriere As Boolean
Const SeparateurTextbox2 = " "
Const NbrDeSeparateursTextbox2 = 4
Const CaracteresPermis = "0123456789"

Private Sub TextBox2_Change()
'Entrée progressive d'un numéro de téléphone "00 00 00 00 00"
Dim i, SuiteDeCaracteres, texte, separateur
If Arriere = True Then
Arriere = False
Exit Sub
End If
If TextBox2.SelStart <> Len(TextBox2.Text) Then
Exit Sub
End If
For i = 1 To Len(TextBox2.Text)
If InStr(CaracteresPermis, Mid(TextBox2.Text, i, 1)) Then
SuiteDeCaracteres = SuiteDeCaracteres + 1
texte = texte & Mid(TextBox2.Text, i, 1)
Else
SuiteDeCaracteres = 0
separateur = separateur + 1
texte = texte & Mid(TextBox2.Text, i, 1)
End If
Next i
If separateur < NbrDeSeparateursTextbox2 Then
If SuiteDeCaracteres = 2 Then
texte = texte & SeparateurTextbox2
End If
End If
TextBox2.Value = texte
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer) 'Telephone
If KeyCode = 8 Then
Arriere = True
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Telephone
A = Chr(KeyAscii)
If InStr(SeparateurTextbox2 & CaracteresPermis, A) = 0 Then
KeyAscii = 0
End If
End Sub

Ma question est : Comment je pourrais faire pour l'utiliser sur
plusieurs texbox (6 dans mon cas), afin de ne pas allourdire le code en
répétant 6 fois la même chose.
J'ai tenté avec des variables donnat le nom des TextBox, et en
remplacant dans le code les endroit des noms du TextBox2 par ces variables
mais en vain.

Je vous remercie par avance pour votre aide, Ouille

4 réponses

Avatar
michdenis
Bonsoir Jacques,

Tu peux passer par un module de classe, il y a un Classeur exemple commenté à télécharger sur le site de Misange : Excelabo.

Si tu préfères, tu peux utiliser ceci :

Pour chacun de tes textbox de ton formulaire, tu ajoutes
ce code en modifiant dans l'appel de la procédure
"PhoneFormat" le nom du textbox

'---------------
Private Sub TextBox1_Change()

PhoneFormat TextBox1

End Sub
'---------------

'---------------
Private Sub TextBox2_Change()

PhoneFormat TextBox2

End Sub
'---------------


Dans un module Standard, tu copies ce qui suit :
cette procédure peut être utile pour tous les formulaires
qui ont besoin de ce format dans un contrôle textbox
'--------------------------------------------
Sub PhoneFormat(TBox As MSForms.TextBox)

Dim A As String, B As Variant
Dim C As Integer, E As String, D As String

If TBox = "" Then Exit Sub
A = Replace(TBox, " ", "")
B = Right(A, 1)
If Not IsNumeric(B) Then
A = Left(A, Len(A) - 1)
End If
For C = 1 To Len(A)
If IsNumeric(Mid(A, C, 1)) Then
E = E & Mid(A, C, 1)
End If
Next
For C = 1 To 8 Step 2
D = D & Mid(E, C, 2) & " "
Next
TBox = Trim(D)
TBox.SelStart = Len(Me.TextBox1)

End Sub
'--------------------------------------------


Salutations!



"Jacques" a écrit dans le message de news:41571667$0$771$
Salut a toutes et tous,

Voila j'ai trouvé ce code sur le forum (Et j'en remercie l'auteur), qui sert
a mettre le format téléphone dans un TextBox.

Dim Arriere As Boolean
Const SeparateurTextbox2 = " "
Const NbrDeSeparateursTextbox2 = 4
Const CaracteresPermis = "0123456789"

Private Sub TextBox2_Change()
'Entrée progressive d'un numéro de téléphone "00 00 00 00 00"
Dim i, SuiteDeCaracteres, texte, separateur
If Arriere = True Then
Arriere = False
Exit Sub
End If
If TextBox2.SelStart <> Len(TextBox2.Text) Then
Exit Sub
End If
For i = 1 To Len(TextBox2.Text)
If InStr(CaracteresPermis, Mid(TextBox2.Text, i, 1)) Then
SuiteDeCaracteres = SuiteDeCaracteres + 1
texte = texte & Mid(TextBox2.Text, i, 1)
Else
SuiteDeCaracteres = 0
separateur = separateur + 1
texte = texte & Mid(TextBox2.Text, i, 1)
End If
Next i
If separateur < NbrDeSeparateursTextbox2 Then
If SuiteDeCaracteres = 2 Then
texte = texte & SeparateurTextbox2
End If
End If
TextBox2.Value = texte
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer) 'Telephone
If KeyCode = 8 Then
Arriere = True
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Telephone
A = Chr(KeyAscii)
If InStr(SeparateurTextbox2 & CaracteresPermis, A) = 0 Then
KeyAscii = 0
End If
End Sub

Ma question est : Comment je pourrais faire pour l'utiliser sur
plusieurs texbox (6 dans mon cas), afin de ne pas allourdire le code en
répétant 6 fois la même chose.
J'ai tenté avec des variables donnat le nom des TextBox, et en
remplacant dans le code les endroit des noms du TextBox2 par ces variables
mais en vain.

Je vous remercie par avance pour votre aide, Ouille
Avatar
michdenis
Bonsoir Jacques,


Tu peux passer par un module de classe, il y a un Classeur exemple dont le code est commenté à télécharger sur le site de
Misange : Excelabo.

Si tu préfères, tu peux utiliser ceci :

Pour chacun de tes textbox de ton formulaire, tu ajoutes
ce code en modifiant dans l'appel de la procédure
"PhoneFormat" le nom du textbox

'---------------
Private Sub TextBox1_Change()

PhoneFormat TextBox1

End Sub
'---------------

'---------------
Private Sub TextBox2_Change()

PhoneFormat TextBox2

End Sub
'---------------


Dans un module Standard, tu copies ce qui suit :
cette procédure peut être utile pour tous les formulaires
qui ont besoin de ce format dans un contrôle textbox
'--------------------------------------------
Sub PhoneFormat(TBox As MSForms.TextBox)

Dim A As String, B As Variant
Dim C As Integer, E As String, D As String
A = TBox
For C = 1 To Len(A)
If IsNumeric(Mid(A, C, 1)) Then
E = E & Mid(A, C, 1)
End If
Next
For C = 1 To 8 Step 2
D = D & Mid(E, C, 2) & " "
Next
TBox = Trim(D)
TBox.SelStart = Len(Me.TextBox1)

End Sub
'--------------------------------------------


Salutations!




"Jacques" a écrit dans le message de news:41571667$0$771$
Salut a toutes et tous,

Voila j'ai trouvé ce code sur le forum (Et j'en remercie l'auteur), qui sert
a mettre le format téléphone dans un TextBox.

Dim Arriere As Boolean
Const SeparateurTextbox2 = " "
Const NbrDeSeparateursTextbox2 = 4
Const CaracteresPermis = "0123456789"

Private Sub TextBox2_Change()
'Entrée progressive d'un numéro de téléphone "00 00 00 00 00"
Dim i, SuiteDeCaracteres, texte, separateur
If Arriere = True Then
Arriere = False
Exit Sub
End If
If TextBox2.SelStart <> Len(TextBox2.Text) Then
Exit Sub
End If
For i = 1 To Len(TextBox2.Text)
If InStr(CaracteresPermis, Mid(TextBox2.Text, i, 1)) Then
SuiteDeCaracteres = SuiteDeCaracteres + 1
texte = texte & Mid(TextBox2.Text, i, 1)
Else
SuiteDeCaracteres = 0
separateur = separateur + 1
texte = texte & Mid(TextBox2.Text, i, 1)
End If
Next i
If separateur < NbrDeSeparateursTextbox2 Then
If SuiteDeCaracteres = 2 Then
texte = texte & SeparateurTextbox2
End If
End If
TextBox2.Value = texte
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer) 'Telephone
If KeyCode = 8 Then
Arriere = True
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Telephone
A = Chr(KeyAscii)
If InStr(SeparateurTextbox2 & CaracteresPermis, A) = 0 Then
KeyAscii = 0
End If
End Sub

Ma question est : Comment je pourrais faire pour l'utiliser sur
plusieurs texbox (6 dans mon cas), afin de ne pas allourdire le code en
répétant 6 fois la même chose.
J'ai tenté avec des variables donnat le nom des TextBox, et en
remplacant dans le code les endroit des noms du TextBox2 par ces variables
mais en vain.

Je vous remercie par avance pour votre aide, Ouille
Avatar
michdenis
Bonjour Jaques,

Une légère correction doit être apportée à la dernière ligne de la procédure générale

En lieu et place de ceci ::
TBox.SelStart = Len(Me.TextBox1)

Cela :
TBox.SelStart = Len(TBox)


La procédure devient alors :
'-----------------------------
Sub PhoneFormat(TBox As MSForms.TextBox)

Dim A As String, B As Variant
Dim C As Integer, E As String, D As String
A = TBox
For C = 1 To Len(A)
If IsNumeric(Mid(A, C, 1)) Then
E = E & Mid(A, C, 1)
End If
Next
For C = 1 To 8 Step 2
D = D & Mid(E, C, 2) & " "
Next
TBox = Trim(D)
TBox.SelStart = Len(TBox)

End Sub
'-----------------------------

Salutations!







"michdenis" a écrit dans le message de news:
Bonsoir Jacques,


Tu peux passer par un module de classe, il y a un Classeur exemple dont le code est commenté à télécharger sur le site de
Misange : Excelabo.

Si tu préfères, tu peux utiliser ceci :

Pour chacun de tes textbox de ton formulaire, tu ajoutes
ce code en modifiant dans l'appel de la procédure
"PhoneFormat" le nom du textbox

'---------------
Private Sub TextBox1_Change()

PhoneFormat TextBox1

End Sub
'---------------

'---------------
Private Sub TextBox2_Change()

PhoneFormat TextBox2

End Sub
'---------------


Dans un module Standard, tu copies ce qui suit :
cette procédure peut être utile pour tous les formulaires
qui ont besoin de ce format dans un contrôle textbox
'--------------------------------------------
Sub PhoneFormat(TBox As MSForms.TextBox)

Dim A As String, B As Variant
Dim C As Integer, E As String, D As String
A = TBox
For C = 1 To Len(A)
If IsNumeric(Mid(A, C, 1)) Then
E = E & Mid(A, C, 1)
End If
Next
For C = 1 To 8 Step 2
D = D & Mid(E, C, 2) & " "
Next
TBox = Trim(D)
TBox.SelStart = Len(Me.TextBox1)

End Sub
'--------------------------------------------


Salutations!




"Jacques" a écrit dans le message de news:41571667$0$771$
Salut a toutes et tous,

Voila j'ai trouvé ce code sur le forum (Et j'en remercie l'auteur), qui sert
a mettre le format téléphone dans un TextBox.

Dim Arriere As Boolean
Const SeparateurTextbox2 = " "
Const NbrDeSeparateursTextbox2 = 4
Const CaracteresPermis = "0123456789"

Private Sub TextBox2_Change()
'Entrée progressive d'un numéro de téléphone "00 00 00 00 00"
Dim i, SuiteDeCaracteres, texte, separateur
If Arriere = True Then
Arriere = False
Exit Sub
End If
If TextBox2.SelStart <> Len(TextBox2.Text) Then
Exit Sub
End If
For i = 1 To Len(TextBox2.Text)
If InStr(CaracteresPermis, Mid(TextBox2.Text, i, 1)) Then
SuiteDeCaracteres = SuiteDeCaracteres + 1
texte = texte & Mid(TextBox2.Text, i, 1)
Else
SuiteDeCaracteres = 0
separateur = separateur + 1
texte = texte & Mid(TextBox2.Text, i, 1)
End If
Next i
If separateur < NbrDeSeparateursTextbox2 Then
If SuiteDeCaracteres = 2 Then
texte = texte & SeparateurTextbox2
End If
End If
TextBox2.Value = texte
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer) 'Telephone
If KeyCode = 8 Then
Arriere = True
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Telephone
A = Chr(KeyAscii)
If InStr(SeparateurTextbox2 & CaracteresPermis, A) = 0 Then
KeyAscii = 0
End If
End Sub

Ma question est : Comment je pourrais faire pour l'utiliser sur
plusieurs texbox (6 dans mon cas), afin de ne pas allourdire le code en
répétant 6 fois la même chose.
J'ai tenté avec des variables donnat le nom des TextBox, et en
remplacant dans le code les endroit des noms du TextBox2 par ces variables
mais en vain.

Je vous remercie par avance pour votre aide, Ouille
Avatar
docm
Bonjour Jacques.

L'auteur remercie michdenis pour l'idée d'utiliser l'exemple de module de
classe de denis michon.

Ce qui donnerait quelque chose comme ce qui suit:

'-------------------------------
'Dans le UserForm, mettre le code suivant
Dim TextBox() As New MesTextBoxs

Private Sub UserForm_Initialize()

'Cette procédure a pour but de définir quels TextBoxs
'du formulaire appartiennent à la nouvelle classe de
'de TextBoxs que nous avons créée.

Dim Nb As Integer, Ctrl As Control

Nb = 0
For Each Ctrl In UserForm1.Controls
If TypeName(Ctrl) = "TextBox" Then
If Ctrl.Name <> "TextBoxAutre" Then
Nb = Nb + 1
ReDim Preserve TextBox(1 To Nb)
Set TextBox(Nb).GroupeTextBox = Ctrl
End If
End If
Next

End Sub
'---------------------------

Insérer un module de classe.
Le nommer MesTextBoxs (Menu Affichage/Propriétés et modifier la propriété
Name)

'---------------------------
'Dans le module de classe, ajouter le code qui suit:
Dim Arriere As Boolean
Const SeparateurGroupeTextBox = " "
Const NbrDeSeparateursGroupeTextBox = 4
Const CaracteresPermis = "0123456789"

'Le NOM du module de classe "MesTextBoxs" est en même
'temps le "NOM" de la nouvelle classe d'objet.

Public WithEvents GroupeTextBox As MSForms.TextBox


Private Sub GroupeTextBox_Change()
'Entrée progressive d'un numéro de téléphone "00 00 00 00 00"
Dim i, SuiteDeCaracteres, texte, separateur
If Arriere = True Then
Arriere = False
Exit Sub
End If
If GroupeTextBox.SelStart <> Len(GroupeTextBox.Text) Then
Exit Sub
End If
For i = 1 To Len(GroupeTextBox.Text)
If InStr(CaracteresPermis, Mid(GroupeTextBox.Text, i, 1)) Then
SuiteDeCaracteres = SuiteDeCaracteres + 1
texte = texte & Mid(GroupeTextBox.Text, i, 1)
Else
SuiteDeCaracteres = 0
separateur = separateur + 1
texte = texte & Mid(GroupeTextBox.Text, i, 1)
End If
Next i
If separateur < NbrDeSeparateursGroupeTextBox Then
If SuiteDeCaracteres = 2 Then
texte = texte & SeparateurGroupeTextBox
End If
End If
GroupeTextBox.Value = texte
End Sub

Private Sub GroupeTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger,
ByVal Shift As Integer) 'Telephone
If KeyCode = 8 Then
Arriere = True
End If
End Sub

Private Sub GroupeTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Telephone
A = Chr(KeyAscii)
If InStr(SeparateurGroupeTextBox & CaracteresPermis, A) = 0 Then
KeyAscii = 0
End If
End Sub
'-------------------------------

Amicalement.


"Jacques" wrote in message
news:41571667$0$771$
Salut a toutes et tous,

Voila j'ai trouvé ce code sur le forum (Et j'en remercie l'auteur), qui
sert

a mettre le format téléphone dans un TextBox.

Dim Arriere As Boolean
Const SeparateurTextbox2 = " "
Const NbrDeSeparateursTextbox2 = 4
Const CaracteresPermis = "0123456789"

Private Sub TextBox2_Change()
'Entrée progressive d'un numéro de téléphone "00 00 00 00 00"
Dim i, SuiteDeCaracteres, texte, separateur
If Arriere = True Then
Arriere = False
Exit Sub
End If
If TextBox2.SelStart <> Len(TextBox2.Text) Then
Exit Sub
End If
For i = 1 To Len(TextBox2.Text)
If InStr(CaracteresPermis, Mid(TextBox2.Text, i, 1)) Then
SuiteDeCaracteres = SuiteDeCaracteres + 1
texte = texte & Mid(TextBox2.Text, i, 1)
Else
SuiteDeCaracteres = 0
separateur = separateur + 1
texte = texte & Mid(TextBox2.Text, i, 1)
End If
Next i
If separateur < NbrDeSeparateursTextbox2 Then
If SuiteDeCaracteres = 2 Then
texte = texte & SeparateurTextbox2
End If
End If
TextBox2.Value = texte
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer) 'Telephone
If KeyCode = 8 Then
Arriere = True
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Telephone
A = Chr(KeyAscii)
If InStr(SeparateurTextbox2 & CaracteresPermis, A) = 0 Then
KeyAscii = 0
End If
End Sub

Ma question est : Comment je pourrais faire pour l'utiliser sur
plusieurs texbox (6 dans mon cas), afin de ne pas allourdire le code en
répétant 6 fois la même chose.
J'ai tenté avec des variables donnat le nom des TextBox, et en
remplacant dans le code les endroit des noms du TextBox2 par ces variables
mais en vain.

Je vous remercie par avance pour votre aide, Ouille