HERBERS
Excel-Forum
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Entdecke rund 2 Millionen Excel-Lösungen im
Forumsarchiv
Forumbeitrag
Excel-Version des Fragestellers:
2010
Erfahrungslevel des Fragestellers:
Basiskenntnisse in VBA
Ulf
10.05.2026 13:15:58
AW: Controls mit Events in Collections verwalten
Hi,
mit Array ~
Modul:
Option Explicit


'Control-Variablen müssen Public sein, wenn sie für die Dauer der Ausführung gelten sollen
'Option Private Module

Public t() As clsTextbox
Public lngTnr As Long

Public Function newControl(ByRef pvobjParent As Object, ByVal pvstrNamen As String, ByVal pvstrType As String, _
Optional ByVal pvsngLeft, Optional ByVal pvsngTop, Optional ByVal pvsngHeight, Optional ByVal pvsngWidth, _
Optional ByVal pvintSFX, Optional ByVal pvintBorderStyle, Optional ByVal pvlngHGFarbe, Optional ByVal pvlngVGFarbe, Optional ByVal pvstrCaption) As Control

Set newControl = pvobjParent.Controls.Add("Forms." & pvstrType & ".1", pvstrNamen, True)
Set t(lngTnr) = New clsTextbox
With newControl
Set t(lngTnr).TxtBox = newControl
If Not IsMissing(pvsngLeft) Then .Left = pvsngLeft
If Not IsMissing(pvsngTop) Then .Top = pvsngTop
If Not IsMissing(pvsngHeight) Then .Height = pvsngHeight
If Not IsMissing(pvsngWidth) Then .Width = pvsngWidth
If Not IsMissing(pvintSFX) Then .SpecialEffect = pvintSFX
If Not IsMissing(pvintBorderStyle) Then .BorderStyle = pvintBorderStyle
If Not IsMissing(pvlngHGFarbe) Then .BackColor = pvlngHGFarbe
If Not IsMissing(pvlngVGFarbe) Then .ForeColor = pvlngVGFarbe
If Not IsMissing(pvstrCaption) Then
On Error Resume Next
.Caption = pvstrCaption
.Text = pvstrCaption
On Error GoTo 0
End If
End With
'Aktuelle Box hochzählen
lngTnr = lngTnr + 1
'
End Function

Userform:
Option Explicit


Private Sub UserForm_Initialize()

'Beispielwerte, ersetzen hier den jeweiligen .Count der Datenklassen:
Const mRows = 3
Const mCols = 3


With Me
.Height = mRows * 20 + 40
.Width = mCols * 60 + 20
.StartUpPosition = 0
.Caption = "GridEditor"
End With
'Aktuelle Box initialisieren
lngTnr = 0
'Anzahl Boxen
ReDim t((mRows * mCols) - 1)
Call fillTbGrid(mRows, mCols)

End Sub

Private Sub fillTbGrid(mRows As Long, mCols As Long)
Dim X As Long, Y As Long

For X = 1 To mRows
For Y = 1 To mCols
Call newControl(Me, "Tb" & CStr(X) & "-" & CStr(Y), _
"TextBox", (Y * 60 - 60), (X * 20 - 20), 23, 63, , 1)
Next Y
Next X

End Sub

Klasse:
Option Explicit


Public WithEvents TxtBox As MSForms.TextBox

Private Sub TxtBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' Select Case TxtBox.Name
' Case "Tb1-1"
'
' Case "Tb3-1"
' '.....
' End Select
MsgBox Me.TxtBox.Name
End Sub

hth
Ulf
Als Antwort auf diesen Beitrag
Sumbu
10.05.2026 07:14:23
AW: Controls mit Events in Collections verwalten
Hallo snb, Hallo Ulf

snb, danke für die eine schön rote Textbox, aber was hat das mit der Überwachung der Events eines Textbox-Arrays zu tun?

Ulf, Du bist schon näher an meinem Problem dran, allerdings erzeugst du genau eine Textbox die du dann in der Watcher-Klasse überwachst. Ich brauche das allerdings für ein Array von Textboxen, damit ich die Werte an eine Datenklasse übergeben kann. Damit Du siehst was ich meine, hab ich mal einen kleinen Beispielcode geschrieben, in dem ich so ein Textbox-Array erszeuge.

Im Userform-Code:
Option Explicit


Private Sub UserForm_Initialize()

'Beispielwerte, ersetzen hier den jeweiligen .Count der Datenklassen:
Const mRows = 10
Const mCols = 5

With Me
.Height = mRows * 20
.Width = mCols * 60
.StartUpPosition = 0
.Caption = "GridEditor"
End With

Call fillTbGrid(mRows, mCols)

End Sub

Private Sub fillTbGrid(mRows As Long, mCols As Long)
Dim X As Long, Y As Long

For X = 1 To mRows
For Y = 1 To mCols
Call newControl(Me, "Tb" & CStr(X) & "-" & CStr(Y), _
"TextBox", (Y * 60 - 60), (X * 20 - 20), 23, 63, , 1)
Next Y
Next X

End Sub


In einem Standardmodul:
Option Explicit


Option Private Module

Public Function newControl(ByRef pvobjParent As Object, ByVal pvstrNamen As String, ByVal pvstrType As String, _
Optional ByVal pvsngLeft, Optional ByVal pvsngTop, Optional ByVal pvsngHeight, Optional ByVal pvsngWidth, _
Optional ByVal pvintSFX, Optional ByVal pvintBorderStyle, Optional ByVal pvlngHGFarbe, Optional ByVal pvlngVGFarbe, Optional ByVal pvstrCaption) As Control

Set newControl = pvobjParent.Controls.Add("Forms." & pvstrType & ".1", pvstrNamen, True)
With newControl
If Not IsMissing(pvsngLeft) Then .Left = pvsngLeft
If Not IsMissing(pvsngTop) Then .Top = pvsngTop
If Not IsMissing(pvsngHeight) Then .Height = pvsngHeight
If Not IsMissing(pvsngWidth) Then .Width = pvsngWidth
If Not IsMissing(pvintSFX) Then .SpecialEffect = pvintSFX
If Not IsMissing(pvintBorderStyle) Then .BorderStyle = pvintBorderStyle
If Not IsMissing(pvlngHGFarbe) Then .BackColor = pvlngHGFarbe
If Not IsMissing(pvlngVGFarbe) Then .ForeColor = pvlngVGFarbe
If Not IsMissing(pvstrCaption) Then
On Error Resume Next
.Caption = pvstrCaption
.Text = pvstrCaption
On Error GoTo 0
End If
End With
End Function


Vielen Dank schonmal und schönen Sonntag



Folgenachrichten
Antwort auf Beitrag erstellen
Bitte einen Anwendernamen ohne @ eingeben.
Bitte das Passwort eingeben.
Bitte eine gültige E-Mail-Adresse eingeben.
Bitte einen Betreff eingeben.
Weitere Optionen
Aktivieren, wenn die Frage/der Beitrag noch nicht beantwortet wurde und unter Listen > Offene Threads erscheinen soll.
Beispieldatei hochladen

Bitte einen Nachrichtentext eingeben.