我有一个带有文本框的用户窗体,放入文本框的任何值都将决定添加到用户窗体的动态控件的数量,然后有一个按钮,一旦点击,我想要动态控件完全从用户表单中删除。
下面显示了用于创建动态控件的代码,此代码可以正常运行
For i = 1 To TextBox1.Value
newPosition = 360
Set cLabel = Me.Controls.Add("Forms.Label.1")
With cLabel
.Caption = "Label " & (i)
.Font.Size = 8
.Font.Bold = True
.Font.Name = "Tahoma"
'.Left = 70
.Left = 36
.Top = switchBoardLevel
.Width = 130
End With
switchBoardLevel = switchBoardLevel + newPosition
Set cButton = Me.Controls.Add("Forms.CommandButton.1")
With cButton
.Name = "CommandButton" & i
.Caption = "Calculate"
.Left = 300
.Top = buttonStartPosition
.Width = 45
.Height = 18
End With
ReDim Preserve TextListBox(1 To i)
Set TextListBox(i).ButtonGroup = cButton
buttonStartPosition = buttonStartPosition + newPosition
Next i
但是,在删除动态创建的控件时会出现问题。我已经尝试了很多方法来删除控件。单击按钮以删除控件时执行下面的代码,但它只是不能为我工作,我会围成一圈,所以如果有人能就此问题给我一些指导,我将不胜感激。 / p>
For Each TextListBox(i).ButtonGroup In Me.Controls
If (TypeOf TextListBox(i).ButtonGroup Is CommandButton) Then
TextListBox(i).ButtonGroup.Visible = False
End If
Next
答案 0 :(得分:1)
你没有提供很多信息,但你无法循环 - 你需要遍历数组:
For i = Lbound(TextListBox) to UBound(TextListBox)
If TypeOf TextListBox(i).ButtonGroup Is MSForms.CommandButton Then
TextListBox(i).ButtonGroup.Visible = False
End If
Next