私はこのエラーが発生しています
「このコンテキスト内でアンロードできません」
彼のような popupmenu からメニュー項目をアンロードしようとするときはいつでも
For i = mnuTCategory.Count - 1 To 1 Step -1
Unload mnuTCategory(i)
Next
このエラーなしでこれを行う方法はありますか?
ありがとう
Form
によってトリガーされたときにからコントロールを削除できるようにするにはComboBox
、 を介して削除操作を実行する必要がありますTimer
。
したがって、イベントがトリガーされる場合は、トリガーされたときに最初に呼び出したいサブルーチンを呼び出すComboBox
a を開始 (有効)します。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