Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

vba - Create event handlers for multiple dynamic controls

I have a userform that creates two dynamic control buttons but I am having difficulty accessing the .name property of the dynamic control, which means I can't create the event handler correctly. Due to this problem I am unable to create event handlers. Below shows the code that creates the dynamic controls and also the code that I have written for the event handlers (which isn't functioning correctly)

Option Explicit

Public WithEvents cButton As MSForms.CommandButton


Private Sub TextBox1_Change()
  If TextBox1 <> vbNullString Then
     For i = 1 To TextBox1.Value        
        Set cButton = Me.Controls.Add("Forms.CommandButton.1")
        With cButton
            .Name = "CommandButton" & i
            .Left = 150
            .Top = buttonStartPosition
            .Width = 300
            .Height = 140
        End With
     Next i
   End If
End sub

 Private Sub cButton_Click()
    If cButton.Name = "CommandButton1" Then
      MsgBox "Button1"
    ElseIf cButton.Name = "CommandButton2" Then
      MsgBox "Button2"
    End If
 End Sub

Once this code is executed and the two buttons are on the screen, I press the first button (button1) and nothing happens but when I press the second button (button2) I receive the message "Button2". So how come I can't access the first button?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@user3538102 .. To your comment regarding Textbox's. Below is example is an example. I added Combo box select either CommandButton or TextBox and generate events. The code works but could be better.

I added combo box to select to dynamically generate object type.

Combo Box

In UserForm Activate event - Add combo drop down list

Private Sub UserForm_Activate()
    ComboBox1.AddItem "CommandButton"
    ComboBox1.AddItem "TextBox"
    ComboBox1.ListIndex = 0
End Sub

In Class1 Class Module ..

ClassModule

Modified UserForm code ..

Option Explicit

Dim cObjs() As New Class1

Private Sub TextBox1_Change()
Dim i As Integer
Dim buttonStartPosition As Integer
Dim cObj As Object

buttonStartPosition = 30

If TextBox1 <> vbNullString Then
 For i = 1 To TextBox1.Value
    If ComboBox1.Value = "CommandButton" Then
        Set cObj = Me.Controls.Add("Forms.CommandButton.1")
    Else
        Set cObj = Me.Controls.Add("Forms.TextBox.1")
    End If

        With cObj
            .Name = ComboBox1.Value & i
            .Left = 15
            .Top = buttonStartPosition
            .Width = 30
            .Height = 14
        End With

    ReDim Preserve cObjs(1 To i)
    If ComboBox1.Value = "CommandButton" Then
        Set cObjs(i).ButtonGroup = cObj
    Else
        Set cObjs(i).TextGroup = cObj
    End If

    buttonStartPosition = buttonStartPosition + 14

 Next i
End If

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...