Code generierte Textboxen in Frames bekommen keinen Focus
25.04.2025 12:46:43
evilscytheman
ich generiere Textboxen in Frames zur Laufzeit in der Initialisierung einer Userform:
Private TextBoxEvents() As New clsTextBoxEvents
Private Sub UserForm_Initialize()
...
Set Frame_DV = Me.Controls.Add("Forms.Frame.1", "Frame_DV")
With Frame_DV
.Top = topPosition
.Left = 10
.width = (controlWidth + controlSpacing) * numControls + tbwidth + 35
.Height = 3 * controlHeight + 60
.Caption = "Design Validation"
.Font.name = "Tahoma"
.Font.Size = 11
.TabStop = False
End With
' Create Frame_DV_Env inside Frame_DV
Set Frame_DV_Env = Frame_DV.Controls.Add("Forms.Frame.1", "Frame_DV_Env")
With Frame_DV_Env
.Top = 10
.Left = 10
.width = (controlWidth + controlSpacing) * numControls + tbwidth + 20
.Height = 1 * controlHeight + 50
.Caption = "Environmental"
.Font.name = "Tahoma"
.Font.Size = 11
.TabStop = False
End With
...
For i = 1 To numControls
Set tb = Frame_DV_Env.Controls.Add("Forms.TextBox.1", "DV_Env_" & i)
With tb
.Top = lblClimate.Top
.Left = startPosition
.width = controlWidth
.Height = controlHeight
.Font.name = "Tahoma"
.Font.Size = 9
.TextAlign = fmTextAlignCenter
End With
startPosition = startPosition + tb.width + controlSpacing
' Initialize and assign the TextBox to the event handler
Set TextBoxEvents(i) = New clsTextBoxEvents
Set TextBoxEvents(i).TextBox = tb
Next i
End Sub
In einem Klassenmodul ("clsTextBoxEvents")definiere ich das Verhalten der Textbox:
Public WithEvents TextBox As MSForms.TextBox
Private Sub TextBox_AfterUpdate()
Call ReplaceDecimalPointWithComma(Me.TextBox)
End Sub
Private Sub TextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call ReplaceDecimalPointWithComma(Me.TextBox)
End Sub
Private Sub TextBox_Change()
Me.TextBox.SetFocus
DoEvents
Debug.Print "Aktuelle ActiveControl: "; UF_AutoSource.ActiveControl.name
Debug.Print Me.TextBox.name
End Sub
Private Sub TextBox_Enter()
Debug.Print "Fokus erlangt: "; Me.TextBox.name
End Sub
Nur die Change Routine wird gestartet, der Rest wird ignoriert. Ich kann aber selbst mit SetFocus in der Change Routine nicht den Focus auf die Textbox legen.
Wie bekommen die Textboxen einen Focus, den ich nutzen kann zur Kontrolle der Eingabe?
Vielen Dank und Grüße
Anzeige