我已经为动态创建文本框和按钮编写了VBA代码。以下是我点击"添加"用户表单上的按钮。
Dim oTxtBox As Control
Dim oBrwsBtn As Control
Dim oCaption As Control
Dim oTxtLen As Integer
oTxtLen1 = TextBox1.Width
oTxtBrth1 = TextBox1.Height
oTxtPos1 = TextBox1.Left
oButLen = CommandButton1.Width
oButBrth = CommandButton1.Height
oTxtLen2 = TextBox2.Width
oTxtBrth2 = TextBox2.Height
If i = Empty Then
i = 1
End If
Set oTxtBox = Me.Controls.Add("Forms.TextBox.1")
Set oBrwsBtn = Me.Controls.Add("Forms.CommandButton.1")
Set oCaption = Me.Controls.Add("Forms.TextBox.1")
With oTxtBox
.Left = oTxtPos1
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oTxtLen1
.Height = oTxtBrth1
End With
With oBrwsBtn
.Left = oTxtPos1 + oTxtLen1 + 18
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oButLen
.Height = oButBrth
End With
With oCaption
.Left = oTxtPos1 + oTxtLen1 + 18 + oButLen + 18
.Top = oTxtBrth1 + 18 + (oTxtBrth + 18) * (i - 1)
.Width = oTxtLen2
.Height = oTxtBrth2
End With
i = i + 1
Q1 现在如何编辑我动态创建的浏览按钮的标题无方法 .Caption
oBrWsBtn
和Q2 :如何在焦点更改时获取值 例如,当我点击' TextBox1'宾语。变量应为自己指定名称(即var(str)=焦点对象名称)
提前致谢
答案 0 :(得分:0)
Q1:为什么不尝试更具体地定义控件类型,看看是否允许您使用。Caption
方法。不明白为什么它现在不起作用,但Control
对象类型可能没有.Caption
方法,因为并非所有控件都有标题。
尝试:
'at the top of the sub:
Dim oBrwsBtn as CommandButton
'then later
Set oBrwsBtn = Me.Controls.Add("Forms.CommandButton.1")
另一个需要考虑的选择是创建这些按钮,让它们在需要之前不可见。但是,假设您实际上没有充分的理由动态创建它们,您可能拥有它们。
Q2:使用control_AfterUpdate sub:
'Dim this wherever appropriate for scope. Could be in the afterupdate sub if that works.
dim TextBox1_Value as string
Private Sub TextBox1_AfterUpdate()
TextBox1_Value = TextBox1.value
End Sub