我收到了这个错误
“无法在此上下文中卸载”
每当我尝试像他一样从弹出菜单中卸载菜单项时
For i = mnuTCategory.Count - 1 To 1 Step -1
Unload mnuTCategory(i)
Next
有没有办法做到这一点没有这个错误>?
谢谢
为了能够从 a 中删除控件Form
,当被 a 触发时ComboBox
,您将需要通过 a 执行删除操作Timer
。
因此,当ComboBox
要触发事件时,启动(启用)一个Timer
在触发时调用您首先要调用的子例程。
这就是代码的样子:
Private Sub MyCombo_Change()
MyTimer.Enabled = False
MyTimer.Enabled = True
End Sub
Private Sub MyTimer_Timer()
MyTimer.Enabled = False
DeleteMenuItems
End Sub
Private Sub DeleteMenuItems()
Dim i As Intener
For i = mnuTCategory.Count - 1 To 1 Step -1
Unload mnuTCategory(i)
Next
End Sub
我下面的测试项目对我来说没有错误,它对你有用吗?
'1 form with :
' 1 command button : name=Command1
' 1 main menu item : name=mnuMain
' 1 sub menu item : name=mnuSub index=0
Option Explicit
Private Sub Command1_Click()
Dim intIndex As Integer
For intIndex = mnuSub.Count - 1 To 1 Step -1
Unload mnuSub(intIndex)
Next intIndex
End Sub
Private Sub Form_Load()
Dim intIndex As Integer
For intIndex = 1 To 3
Load mnuSub(intIndex)
mnuSub(intIndex).Caption = "Sub" & CStr(intIndex)
Next intIndex
End Sub
有趣的!下面的 testproject 给出了同样的错误:它确实是由从组合框调用卸载引起的。
'1 form with :
' 1 combobox : name=Combo1
' 1 main menu item : name=mnuMain
' 1 sub menu item : name=mnuSub index=0
Option Explicit
Private Sub Combo1_Click()
Dim intIndex As Integer
With Combo1
Select Case .ListIndex
Case 0 'add
For intIndex = 1 To 3
Load mnuSub(intIndex)
mnuSub(intIndex).Caption = "Sub" & CStr(intIndex)
Next intIndex
Case 1 'del
For intIndex = mnuSub.Count - 1 To 1 Step -1
Unload mnuSub(intIndex)
Next intIndex
End Select
End With 'Combo1
End Sub
Private Sub Form_Load()
With Combo1
.AddItem "add"
.AddItem "del"
End With 'Combo1
End Sub
这让我很感兴趣,但我找不到比使用另一个控件更简洁的解决方案,这个控件可以是您已经在表单上拥有的控件,也可以是仅用于此目的的虚拟控件。然后您可以使用组合框的 lostfocus 事件
请参阅下面的测试项目:
'1 form with :
' 1 combobox : name=Combo1
' 1 textbox : name=Text1
' 1 main menu item : name=mnuMain
' 1 sub menu item : name=mnuSub index=0
Option Explicit
Private Sub Combo1_Click()
Dim intIndex As Integer
With Combo1
Select Case .ListIndex
Case 0 'add
For intIndex = 1 To 3
Load mnuSub(intIndex)
mnuSub(intIndex).Caption = "Sub" & CStr(intIndex)
Next intIndex
Case 1 'del
Text1.SetFocus
End Select
End With 'Combo1
End Sub
Private Sub Combo1_LostFocus()
'use the lostfocus event to unload stuff
Dim intIndex As Integer
For intIndex = mnuSub.Count - 1 To 1 Step -1
Unload mnuSub(intIndex)
Next intIndex
End Sub
Private Sub Form_Load()
With Combo1
.AddItem "add"
.AddItem "del"
End With 'Combo1
End Sub
Private Sub Text1_GotFocus()
Combo1.SetFocus
End Sub