السلام عليكم،
محاولة جديدة ولكن ما زالت منقوصة .
يحيث يعمل الزر BackSpace فقط عندما نمحو الارقام من الآخر.
اما من الوسط فلا يتم مسح الا رقمين على اقصى حد بالنسبة للزرين.
ويتم دائما اضافة الارقام في آخر وضعية من TextBox وليس في مكان الارقام الممحوة.
Option Explicit
Dim LeTexte As String
Dim chargement As Boolean
Dim posinex As Byte
Dim flag As Boolean
Dim poscur As Byte
Private Sub TextBox1_Change()
Dim Val As String
Dim Ind As Byte
Dim i As Byte
If flag = True Then Exit Sub
i = TextBox1.SelStart
If i < poscur Then
TextBox1.SelStart = i
poscur = i
Label1.Caption = i - 1
Exit Sub
End If
formatagetextbox
TextBox1.SelStart = 12
poscur = 12
For i = 1 To 12
If Mid(TextBox1, i, 1) = "_" Then
TextBox1.SelStart = i - 1
poscur = i - 1
Label1.Caption = i - 1
Exit For
End If
Next i
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Or _
Len(Replace(TextBox1, "_", "")) = 12 Then
KeyAscii = 0
Exit Sub
End If
End Sub
Private Sub formatagetextbox()
Dim Data1 As String
Dim Data2 As String
Dim i As Byte
Dim j As Byte
flag = True
' formatage des données
Data1 = Replace(TextBox1, "_", "")
Data1 = Application.WorksheetFunction.Trim(Data1)
Data1 = Replace(Data1, " ", "")
j = 1
For i = 1 To 12
Select Case i
Case 1, 2, 3, 5, 6, 8, 9, 11, 12
If IsNumeric(Mid(Data1, j, 1)) Then
Data2 = Data2 & Mid(Data1, j, 1)
j = j + 1
Else
Data2 = Data2 & "_"
End If
Case 4, 7, 10
Data2 = Data2 & " "
End Select
Next i
TextBox1 = Data2
flag = False
End Sub
Private Sub UserForm_Initialize()
LeTexte = "___ __ __ __"
' 123456789012
TextBox1.Value = LeTexte
TextBox1.SelStart = 0
End Sub