更改动态创建的UserForm元素的值

如何解决更改动态创建的UserForm元素的值

这是对我的first question的跟踪:

通过点击事件,我将一些元素(txtBox01cmdButton01)动态添加到了以前的空白(静态)UserForm1中。现在,我想通过cmdButton01的click事件来更改文本框的内容。我究竟该如何引用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

我通过以下代码为click事件分配了代码。但是我不确定如何在静态表单上引用动态元素。我尝试了一些在网上找到的示例,但是没有用:

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. 插入一个library(shiny) library(shinyjs) # Define UI ui <- fluidPage( useShinyjs(),# Application title titlePanel("Demo"),# Sidebar sidebarLayout( sidebarPanel( first_module_ui("first") ),mainPanel( second_module_ui("second") ) ) ) # Define server logic server <- function(input,output,session) { first_module_res <- callModule(first_module_server,"first") observe( function_result <- user_function(first_module_res),second_module_res <- callModule(second_module_server,"second",function_result) ) } # Run the application shinyApp(ui = ui,server = server) 模块,将其命名为Class并复制下一个代码:
clsBtn
  1. 在“表单”模块顶部的“声明”区域中,粘贴下一个变量声明:
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 事件将如下所示:
CommandButton1_Click
  1. 加载表单,单击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 ,然后单击新创建的按钮(“保存”标题)。它将新创建的文本框从“已更改”中的“某物”中更改。
,

要回答您的特定问题,语法如下:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-