从弹出菜单中卸载项目错误

时间:2013-01-20 08:52:37

标签: vb6 popupmenu

我收到此错误

  

“无法在此上下文中卸载”

当我尝试从像他的

这样的弹出菜单中卸载一个菜单项时
For i = mnuTCategory.Count - 1 To 1 Step -1
       Unload mnuTCategory(i)
Next

有没有办法在没有此错误的情况下执行此操作>?

由于

3 个答案:

答案 0 :(得分:2)

为了能够从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 :(得分:0)

我下面的测试项目对我来说没有错误,它对你有用吗?

'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

答案 2 :(得分:0)

这引起了我的兴趣,但我找不到比使用其他控件更清晰的解决方案,这个控件可以是你已经在你的表单上的控件,或者只是为了这个目的的虚拟控件。然后你可以使用组合框的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