Module de classe pour TextBox
Bonjour à tous,
J'ai crée un module de classe pour remplacer un traitement répétitif dans l'événement KeyDown des TextBox.
Mais voila, parfois les événements KeyDown des TextBox, contiennent des valeurs différentes et parfois des lignes de commande en plus l'un par rapport à l'autre.
Comment unifier le module de classe, tout en gardant les lignes de codes ou les quelques propriétés en plus.
- Les .Text parfois elle est égal à "", "T" ou "L3-"
- On trouve parfois Selstart dans des Textbox et pas d’autres
Merci.
Les Evenements à remplacer.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| Private Sub tbN_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
KeyCode = 0
With tbTr
.Text = "T"
.SelStart = Len(.Text)
.SetFocus
End With
End If
End Sub
Private Sub tbTr_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim Texte As String
Texte = tbTr.Text
If Len(Texte) = 4 Then Texte = Texte & "-"
tbTr.Text = Texte
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
KeyCode = 0
With tbPr
.Text = ""
.SetFocus
End With
End If
End Sub
Private Sub tbPr_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
With tbL3
KeyCode = 0
.Text = "L3-"
.SelStart = Len(.Text)
.SetFocus
End With
End If
End Sub
Private Sub tbAD_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
With tbPt
KeyCode = 0
.Text = ""
.SetFocus
End With
End If
End Sub |
-----
Classe : KeyControlClass-----
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| Public WithEvents tbKey As MSForms.TextBox
Private Sub tbKey_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Or KeyCode = vbKeyTab Then
KeyCode = 0
With tbTr
.Text = "T" ' on trouve parfois .Text="", parfois .Text = "L3-" : Comment traiter ce probleme ?
.SelStart = Len(.Text)
.SetFocus
End With
End If
End Sub |
-----
Appel de la classe
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Dim TBx(1 To 5) As New keyControlClass
Private Sub UserForm_Initialize()
Dim b As Byte
Dim Ctl As MSForms.Control
b = 1
For Each Ctl In Me.Controls
If TypeOf Ctl Is MSForms.TextBox Then
TBx(b).tbKey = Ctl.Name
b = b + 1
End If
Next Ctl
End Sub |
Mais quand j'appel mon userform avec FrmSasie.Show ça bug.
Une solution ?
Merci d'avance.