Private Sub UserForm_Activate() Dim nb As Integer, obj As Control, i As Integer Me.Move 0, 0, 500, 400 Dim boitetexte As MSForms.Control On Error Resume Next For i = 0 To 2 Set boitetexte = Me.Controls("" & i) If boitetexte Is Nothing Then Set boitetexte = Me.Controls.Add("Forms.TextBox.1", "toto(" & i & ")", True) With boitetexte .Caption = "Ma textbox" & i .Top = 20 * i .Left = 100 * i .Height = 20 .Width = 100 End With Else MsgBox "existe déjà !" End If Set boitetexte = Nothing Next For Each obj In Me.Controls If obj.Name Like "toto*" Then nb = nb + 1 ReDim Preserve mestextboxes(1 To nb) Set mestextboxes(nb).TextBoxEvents = obj End If Next obj Set obj = Nothing End Sub
Public mestextboxes() As New Classe1
Option Explicit Public WithEvents TextBoxEvents As MSForms.TextBox Private Sub TextBoxEvents_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim index As Integer, qui As String qui = UserForm1.ActiveControl.Name index = Val(Mid(qui, InStr(qui, "(") + 1)) Select Case index Case 0 If KeyAscii <> 8 And Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0 End If Case 1 If IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0 End If Case 2 KeyAscii = Asc(UCase(Chr(KeyAscii))) End Select End Sub