我收到此错误
"无法在此上下文中卸载">
当我尝试像他一样从弹出菜单中卸载菜单项时
For i = mnuTCategory.Count - 1 To 1 Step -1
Unload mnuTCategory(i)
Next
有没有办法在没有此错误的情况下做到这一点>?
谢谢
能够从Form
中删除控件,当由ComboBox
触发时,您需要通过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
编辑
有趣!下面的测试项目给出了相同的错误:它确实是由从组合框调用卸载引起的。
'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