更改动态创建的用户窗体元素的值

Change value of dynamically created UserForm element

这是我的 的跟进:

通过一个点击事件,我动态地添加了一些元素(txtBox01cmdButton01)到之前空的(静态的)UserForm1。现在我想通过cmdButton01的点击事件改变文本框的内容。我究竟要如何引用 cmdButton01?

以下是我创建动态元素的方法(已简化!):

Private Sub CommandButton1_Click()    
    
    Dim cmdArray() As New Class1
    i = 1       
        
        'Layout for static Form
            'Set Formsize / Formtitle
                UserForm1.Height = 130
                UserForm1.Width = 300

            'Create Form-Elements (TextBox1)
                Dim txtBox01 As MSForms.TextBox
                Set txtBox01 = UserForm1.Controls.Add("Forms.TextBox.1", "dynTxtBox_01")
                txtBox01.Top = 10
                txtBox01.Left = 10
                txtBox01.Width = 200
                txtBox01.Text = "something"

            'Create Form-Elements (Commandbutton)
                Dim cmdButton01 As MSForms.CommandButton
                Set cmdButton01 = UserForm13.Controls.Add("Forms.CommandButton.1", "dynCmdButton01", False)
                cmdButton01.Top = 70
                cmdButton01.Left = 10
                cmdButton01.Width = 200
                cmdButton01.Caption = "Save"
                cmdButton01.Visible = True

                ReDim Preserve cmdArray(1 To i)
                Set cmdArray(i).CmdEvents = cmdButton01
                Set cmdButton01 = Nothing                    

        'Show Form
            UserForm1.Show

    End Sub

我通过以下代码分配了点击事件的代码。但我不确定如何在静态表单上引用动态元素。我尝试了几个在网上找到的示例,但没有任何效果:

Public WithEvents CmdEvents As MSForms.CommandButton    
Private Sub CmdEvents_Click()

    'Simple Test (works fine)
        MsgBox "Test1"

    'Change the Text of TextBox01 (this one is PSEUDO code to illustrate what I want to do)
         UserForm1.txtBox01.Text= "123"       
         '=> how should I reference the dynamic form element to make this work??
         
     'Close Form
        UserForm1.Hide

    End Sub

请使用下一个方法:

  1. 插入一个Class模块,命名为clsBtn并复制下一段代码:
Option Explicit

Public WithEvents cmdButton As MSForms.CommandButton

Public Sub cmdButton_Click()
    Dim ans As String
    ans = InputBox("What to write in the newly created text box?", _
                            "Write some text, please", "Default")
    If ans <> "" Then
      cmdButton.Parent.txtBox01.Text = ans
    End If
End Sub
  1. 在表单模块顶部的声明区域中,粘贴下一个变量声明:
Public txtBox01 As MSForms.TextBox
Private cmdButton01 As MSForms.CommandButton
Private ButtColl As New Collection
Private cmdButt(0) As New clsBtn
  1. 您的 CommandButton1_Click 活动将如下所示:
Private Sub CommandButton1_Click()
    Set txtBox01 = Me.Controls.Add("Forms.TextBox.1", "dynTxtBox_01")
    With txtBox01
        .top = 10
        .left = 10
        .width = 200
        .Text = "something"
    End With
    
    Set cmdButton01 = Me.Controls.Add("Forms.CommandButton.1", "dynCmdButton01", False)
    With cmdButton01
        .top = 70
        .left = 10
        .width = 200
        .Caption = "Save"
        .Visible = True
    End With
    
    ButtColl.Add cmdButton01, cmdButton01.Name
    Set cmdButt(0).cmdButton = cmdButton01
End Sub
  1. 加载表单,单击 CommandButton1,然后单击新创建的按钮(“保存”标题)。它将从“已更改”中的“某物”更改新创建的文本框...

要回答您的具体问题,语法如下:

UserForm1.Controls("dynTxtBox_01").Text = "123"