Pregunta

i have am getting this error

"Unable to unload within this context"

when ever i try to unload a menu item from the popupmenu like his

For i = mnuTCategory.Count - 1 To 1 Step -1
       Unload mnuTCategory(i)
Next

Is there any way to do this without this error>?

Thanks

¿Fue útil?

Solución

In order to be able to remove controls from a Form, when triggered by a ComboBox, you will need to execute the deletion operation through a Timer.

So, when the ComboBox event is to be triggered, start (enable) a Timer that when triggered, calls the subroutine that you wanted to call in the first place.

This is how the code would look like:

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

Otros consejos

my test project below works without errors for me, does it work for you ?

'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

Edit

funny! the testproject below gives the same error : it is indeed caused by calling the unload from an combobox ..

'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

this intrigues me but i can't find a cleaner solution than using another control, this control can be an control which you already have on your form, or a dummy control just for this purpose. you can then use the lostfocus event of the combobox

see the testproject below :

'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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top