Thema Datum  Von Nutzer Rating
Antwort
10.03.2020 12:31:32 Sebastian
NotSolved
10.03.2020 12:32:31 Gast96907
NotSolved
10.03.2020 15:35:25 Gast73147
NotSolved
10.03.2020 17:21:39 Gast0815
NotSolved
10.03.2020 17:25:06 Gast01234
NotSolved
11.03.2020 19:40:39 xlKing
NotSolved
11.03.2020 20:03:32 Mase
NotSolved
17.03.2020 15:51:03 Sebastian
NotSolved
Rot Userform mit dynamischen Labeln - Funktion zuweisen
17.03.2020 20:00:10 Gast48180
***
NotSolved
17.03.2020 20:02:24 Gast9933
***
NotSolved
18.03.2020 19:25:32 xlKing
NotSolved
19.03.2020 13:32:29 Trägheit
NotSolved
19.03.2020 14:08:17 Mase
NotSolved
19.03.2020 19:55:09 Trägheit
NotSolved

Ansicht des Beitrags:
Von:
Gast48180
Datum:
17.03.2020 20:00:10
Views:
549
Rating: Antwort:
  Ja
Thema:
Userform mit dynamischen Labeln - Funktion zuweisen

Wie schon darauf hingewiesen wurde, ist das nicht ganz so einfach/trivial:

z.B. UserForm1:

'
'in UserForm
'
Option Explicit

Private WithEvents m_objCtlWrapper As VBAProject.CtlWrapper

Private Sub CommandButton1_Click()
  
  Dim i As Long
  
  For i = 1 To 3
    
    With m_objCtlWrapper.AddControl(UserForm1, "Forms.Label.1", "lblTest" & Format$(i, "00"), True)
      .Caption = "Test" & Format$(i, "00")
      .Left = 10
      .Width = 50
      .Top = 30 * i
    End With
    
  Next
  
End Sub

Private Sub m_objCtlWrapper_OnClick(ByVal Control As MSForms.Control)
  
  If TypeOf Control Is MSForms.Label Then
    
    MsgBox "Label_Click {Name: '" & Control.Name & "', Caption: '" & Control.Caption & "'}", vbInformation
    
  End If
  
End Sub

Private Sub UserForm_Initialize()
  Set m_objCtlWrapper = New VBAProject.CtlWrapper
End Sub

Private Sub UserForm_Terminate()
  m_objCtlWrapper.RemoveAll
  Set m_objCtlWrapper = Nothing
End Sub

Klasse: CtlWrapper
 

'
'in Klassenmodul: CtlWrapper
'
Option Explicit

Event OnClick(ByVal Control As MSForms.Control)

Private m_colControls As VBA.Collection

Public Function AddControl(Container As MSForms.UserForm, ProgID As String, Optional Name, Optional Visible) As MSForms.Control
  
  Const E_NOTIMPL = &H80004001
  
  Dim ctl As Object
  
  Select Case UCase$(ProgID)
    
    Case "FORMS.LABEL.1"
      Set ctl = New VBAProject.CtlLabel
      Set ctl.Control = Container.Controls.Add(ProgID, Name, Visible)
      Set ctl.Wrapper = Me
      
'    Case "..."
      '...
      
    Case Else
      Err.Raise E_NOTIMPL, TypeName(Me) & "::AddControl()", "invalid or not supported ProgID"
  End Select
  
  Call m_colControls.Add(ctl)
  
  If IsMissing(Name) Then Name = ctl.Control.Name
  If IsMissing(Visible) Then Name = ctl.Control.Visible
  
  Set AddControl = ctl.Control
  
End Function

Public Sub Remove(ByVal Control As Object)
  If Not TypeOf Control Is MSForms.Control Then Exit Sub
  Dim i As Long
  For i = 1 To m_colControls.Count
    If m_colControls(i).Control Is Control Then
      Call m_colControls.Remove(i)
      Exit Sub
    End If
  Next
  Err.Raise 9, TypeName(Me) & "::Remove()", "cannot remove control; not in list" 'index out of range
End Sub

Public Function RemoveAll()
  Dim ctl As Object
  For Each ctl In m_colControls
    Set ctl.Control = Nothing
    Set ctl.Wrapper = Nothing
  Next
  Set m_colControls = New VBA.Collection
End Function

Friend Sub InvokeEvent(ByVal EventType As String, ByVal Caller As MSForms.Control)
  Select Case LCase$(EventType)
    Case "click": RaiseEvent OnClick(Caller)
'    Case ...: RaiseEvent ...
  End Select
End Sub

Private Sub Class_Initialize()
  Set m_colControls = New VBA.Collection
End Sub

Private Sub Class_Terminate()
  Set m_colControls = Nothing
End Sub

Klasse: CtlLabel

'
'in Klassenmodul: CtlLabel
'
Option Explicit

Private WithEvents m_objMSFLabel As MSForms.Label

Private m_objWrapper As VBAProject.CtlWrapper


Public Property Get Control() As MSForms.Control
  Set Control = m_objMSFLabel
End Property

Friend Property Set Control(ByVal RHS As MSForms.Control)
  Set m_objMSFLabel = RHS
End Property

Public Property Get Wrapper() As VBAProject.CtlWrapper
  Set Wrapper = m_objWrapper
End Property

Friend Property Set Wrapper(ByVal RHS As VBAProject.CtlWrapper)
  Set m_objWrapper = RHS
End Property

'### EVENTs ###

Private Sub m_objMSFLabel_Click()
  m_objWrapper.InvokeEvent "click", m_objMSFLabel
End Sub

 

Grüße


Ihre Antwort
  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen
Thema: Name: Email:



  • Bitte beschreiben Sie Ihr Problem möglichst ausführlich. (Wichtige Info z.B.: Office Version, Betriebssystem, Wo genau kommen Sie nicht weiter)
  • Bitte helfen Sie ebenfalls wenn Ihnen geholfen werden konnte und markieren Sie Ihre Anfrage als erledigt (Klick auf Häckchen)
  • Bei Crossposting, entsprechende Links auf andere Forenbeiträge beifügen / nachtragen
  • Codeschnipsel am besten über den Code-Button im Text-Editor einfügen
  • Die Angabe der Emailadresse ist freiwillig und wird nur verwendet, um Sie bei Antworten auf Ihren Beitrag zu benachrichtigen

Thema Datum  Von Nutzer Rating
Antwort
10.03.2020 12:31:32 Sebastian
NotSolved
10.03.2020 12:32:31 Gast96907
NotSolved
10.03.2020 15:35:25 Gast73147
NotSolved
10.03.2020 17:21:39 Gast0815
NotSolved
10.03.2020 17:25:06 Gast01234
NotSolved
11.03.2020 19:40:39 xlKing
NotSolved
11.03.2020 20:03:32 Mase
NotSolved
17.03.2020 15:51:03 Sebastian
NotSolved
Rot Userform mit dynamischen Labeln - Funktion zuweisen
17.03.2020 20:00:10 Gast48180
***
NotSolved
17.03.2020 20:02:24 Gast9933
***
NotSolved
18.03.2020 19:25:32 xlKing
NotSolved
19.03.2020 13:32:29 Trägheit
NotSolved
19.03.2020 14:08:17 Mase
NotSolved
19.03.2020 19:55:09 Trägheit
NotSolved