Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Unload Menu Items at run time

Status
Not open for further replies.

nwb1

Programmer
Apr 28, 2004
39
GB
Can any one help me on this please?
I have a popup menu in my application, where I need to load/remove items at runtime.

First I load the menu in form Load() event and later I need to re-create the menu by unloading previous menu items. I'm using following code but there seems a problem with it. Can anyone let me know how to solve this please?
Thanks in advance.

In Form Load()
mnuItem = mnuItem + 1
dim i as integer
for i=1 to 20
Load MyMenu(mnuItem)
MyMenu.Item(mnuItem).Caption = "Item " & i
next

In Command1_Click
Dim i as integer
For i = MyMenu.UBound To 2 Step -1
Unload MyMeny.Item(i)
Next
'now need to load new items
for i=1 to 30
load myMenu(i)
MyMenu.Item(i).Caption = "New Item " & i
next
 
It would have helped if you had mentioned what problem you were experiencing, and how your menus were set up.

Having created a project to test this,the problems in the code were these:

MyMenu spelled MyMeny (put Option Explicit at the top of your code)

Load MyMenu(menuitem) instead of Load MyMenu(i)
(menu item counter is not used...)

.Ubound to 2... should be .Ubound to 1 because you left behind MyMenu(1)


The working code is:
Code:
Private Sub Command1_Click()

  Dim i As Integer
  For i = MyMenu.UBound To 1 Step -1
      Unload MyMenu.Item(i)
  Next
 'now need to load new items
 For i = 1 To 30
     Load MyMenu(i)
     MyMenu.Item(i).Caption = "New Item " & i
 Next
End Sub
Code:
Private Sub Form_Load()
  Dim i As Integer
   For i = 1 To 20
       Load MyMenu(i)
       MyMenu.Item(i).Caption = "Item " & i
  Next
End Sub
 
Thanks for your help. I'll try that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top