如何解决更改动态创建的UserForm元素的值
这是对我的first question的跟踪:
通过点击事件,我将一些元素(txtBox01
和cmdButton01
)动态添加到了以前的空白(静态)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
解决方法
请使用下一种方法:
- 插入一个
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
- 在“表单”模块顶部的“声明”区域中,粘贴下一个变量声明:
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
- 您的
Public txtBox01 As MSForms.TextBox Private cmdButton01 As MSForms.CommandButton Private ButtColl As New Collection Private cmdButt(0) As New clsBtn
事件将如下所示:
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
,然后单击新创建的按钮(“保存”标题)。它将新创建的文本框从“已更改”中的“某物”中更改。
要回答您的特定问题,语法如下:
UserForm1.Controls("dynTxtBox_01").Text = "123"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。