【问题标题】:Change value of dynamically created UserForm element更改动态创建的 UserForm 元素的值
【发布时间】:2020-12-09 01:42:22
【问题描述】:

这是对我的first question 的跟进:

通过点击事件,我将一些元素(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

【问题讨论】:

  • 可以使用以下语法UserForm1.Controls("dynTxtBox_01").Text = "123"
  • @BrianMStafford 工作正常,唯一的问题是我必须使用Unload UserForm1 否则 - 以修改后的形式 - 它仅适用于“第一次通话”。知道这是为什么吗?
  • @BrianMStafford 顺便说一句,请随时将其发布为答案...

标签: vba dynamic userform


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    请使用下一种方法:

    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. 在 Form 模块顶部的声明区域中,粘贴下一个变量声明:
    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" 效果很好。 (只是我不得不使用Unload UserForm1 的一个小问题,否则它会在第一次通话后停止工作)
    • @Albin:在你提问前十二小时。在我尝试展示如何处理新创建的文本框值更改之前的 11 小时。同时,如果您找到了更好的选择,请随意使用!上述问题的答案将是下一个:使用cmdButton.Parent.txtBox01.Text = "Changed" 将为您提供将表单名称从默认的UserForm1 更改为MyForm 的可能性,而无需更改类代码。表单对象由cmdButton.Parent 返回,与表单名称无关。而“这种方法”意味着更多......
    • @Albin:我更新了上面的答案以提供选择要传递给新创建的文本框的文本的可能性。
    • 对不起,没有忘恩负义的意思,只是想问一下,因为没有直接评论好处是什么。我从 Brian 那里得到了“我的”解决方案(请参阅问题的 cmets)。我现在看看你的,让你知道它是如何工作的。
    • @Albin:当然可以。该行属于您的原始代码。第三步仅替换您的原始代码,以便将事件分配给新创建的按钮。在这些行中:ButtColl.Add cmdButton01, cmdButton01.NameSet cmdButt(0).cmdButton = cmdButton01
    猜你喜欢
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多