Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionDim Masque, Caractère, St As String Dim ChrsAffichés, Compteur, i, Rt As Integer Dim ToucheSupp As Boolean Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) SelectNext End Sub Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If IsNumeric(Chr(KeyAscii)) = True Then If KeyAscii <> 8 Then TextBox1.Tag = TextBox1.Tag & Chr(KeyAscii) Formate End If Else If KeyAscii = 8 Then St = TextBox1.Tag If Len(St) > 0 Then St = Replace(St, " ", "") St = Left(St, Len(St) - 1) TextBox1.Tag = St Formate End If End If End If 'L'unique touche qui sera validée sera le BackSpace, le reste sera traité ci-dessus If KeyAscii = 8 Then If Len(TextBox1.Tag) < 1 Then KeyAscii = 0 Else KeyAscii = 0 End If End Sub Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = 46 Then Formate SelectNext End Sub Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) SelectNext End Sub Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'On prend en compte uniquement la touche Backspace, les espaces et les "#" If KeyAscii <> 35 And KeyAscii <> 32 And KeyAscii <> 8 Then KeyAscii = 0 End Sub Private Sub TextBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 'On supprime les éventuelles erreurs de double espaces TextBox2.Text = Replace(TextBox2.Text, " ", " ") Masque = TextBox2.Text Formate End Sub Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) 'on prend en compte uniquement les chiffres et la touche BackSpace If IsNumeric(Chr(KeyAscii)) False And KeyAscii <> 8 Then KeyAscii 0 End Sub Private Sub TextBox3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 'Au changement de la valeur on inscrit automatiquement le masque 'A l'utilisateur d'y inscrire les espaces après If IsNumeric(TextBox3.Text) = True Then TextBox2.Text = "" For Rt = 1 To Val(TextBox3.Text) TextBox2.Text = TextBox2.Text & "#" Next Rt Masque = TextBox2.Text Formate End If End Sub Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If IsNumeric(Chr(KeyAscii)) False And KeyAscii <> 8 Then KeyAscii 0 End Sub Private Sub TextBox4_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 'Determine le nombre de caractères qui doivent rester visibles au début du texte If IsNumeric(TextBox4.Text) = True Then If Val(TextBox4.Text) > Val(TextBox3.Text) Then TextBox4.Text = Val(TextBox3.Text) ChrsAffichés = Val(TextBox4.Text) Formate End If End Sub Private Sub TextBox5_Change() 'Determine le caractère de substituition du masque Caractère = TextBox5.Text Formate End Sub Private Sub TextBox5_Enter() TextBox5.SelStart = 0 TextBox5.SelLength = 1 End Sub Private Sub TextBox5_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) If KeyAscii 8 Then KeyAscii 0 End Sub Private Sub UserForm_Initialize() Masque = TextBox2.Text Caractère = TextBox5.Text ChrsAffichés = Val(TextBox4.Text) Formate TextBox1.SelStart = 0 TextBox1.SelLength = 0 TextBox1.MaxLength = Len(Masque) TextBox5.MaxLength = 1 TextBox4.MaxLength = 1 End Sub Private Sub SelectNext() St = TextBox1.Tag St = Replace(St, " ", "") Compteur = 0 'On sélectionne le prochain emplacement For i = 1 To Len(TextBox1.Text) If Mid(TextBox1.Text, i, 1) <> " " Then Compteur = Compteur + 1 If Compteur > Len(St) Then TextBox1.SetFocus TextBox1.SelStart = i - 1 TextBox1.SelLength = 0 Exit For End If End If Next i 'Si le numéro est complet on déselectionne le texte 'et le contenu de textbox1.Tag est prêt à être utilisé If Len(TextBox1.Tag) = Len(Masque) Then TextBox1.SelStart = Len(TextBox1.Text) TextBox1.SelLength = 0 'List1.AddItem textbox1.Tag End If End Sub Private Sub Formate() i = 0 St = "" 'On supprime les espaces TextBox1.Tag = Replace(TextBox1.Tag, " ", "") 'On remet le tout au format pré-défini For Rt = 1 To Len(Masque) If Mid(Masque, Rt, 1) = "#" Then i = i + 1 If i > Len(TextBox1.Tag) Then Exit For St = St & Mid(TextBox1.Tag, i, 1) Else St = St & " " End If Next Rt TextBox1.Tag = St 'Affichage du chiffre à titre d'exemple Me.Caption = "Le numéro est le: " & TextBox1.Tag 'On remet le textbox1 au même format mais avec les caractères de 'subtituition et en tenant compte des chiffre qui doivent rester visibles St = "" TextBox1.Text = "" i = 0 For Rt = 1 To Len(TextBox1.Tag) If Mid(TextBox1.Tag, Rt, 1) = " " Then St = St & " " Else i = i + 1 If i > ChrsAffichés Then St = St & Caractère Else St = St & Mid(TextBox1.Tag, Rt, 1) End If End If Next Rt 'On remplit le restant du textbox1 conformément au masque If Len(TextBox1.Tag) < Len(Masque) Then For Rt = Len(TextBox1.Tag) + 1 To Len(Masque) If Mid(Masque, Rt, 1) = " " Then St = St & " " Else St = St & Caractère End If Next Rt End If TextBox1.Text = St End Sub
En premier, tu crées une Textbox ensuite tu clones toute la forme.
Donc le "Fantôme" est la 8eme TextBox (TextBox8) créée dans laquelle on manipule les masques.
Corriges-moi si je me trompe
Set fantome = F.Controls.Add("Forms.TextBox.1")
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode > 95 And KeyCode < 108 Then If KeyCode <> 8 Then TextBox1.Tag = TextBox1.Tag & Chr(KeyCode - 48) Formate End If Else If KeyCode = 8 Then St = TextBox1.Tag If Len(St) > 0 Then St = Replace(St, " ", "") St = Left(St, Len(St) - 1) TextBox1.Tag = St Formate End If End If End If 'L'unique touche qui sera validée sera le BackSpace, le reste sera traité ci-dessus If KeyCode = 8 Then If Len(TextBox1.Tag) < 1 Then KeyCode = 0 Else KeyCode = 0 End If End Sub
Private Sub TextBox5_Enter() TextBox5.SelStart = 0 TextBox5.SelLength = 1 End Sub
Set fantome = F.Controls.Add("Forms.TextBox.1")
Set Fac = F
En règle générale, lorsque vous utilisez l'instruction Set pour associer une référence d'objet à une variable, aucune copie de l'objet n'est créée pour cette variable
J'aimerais appliquer un masque pour la saisie d'un numéro de téléphone de la sorte :
"011 __ __ __"
Le terme module en programmation identifie une structure de programmation.