Iterate thru menuitems of a contextmenu

  • Thread starter Thread starter Jorge Cavalheiro
  • Start date Start date
J

Jorge Cavalheiro

Hello
I am trying to browse thru all menuitens of contextmenus.
The form has 3 contextmenus.

mi and mi2 as menuitems, f is a form

For Each mi In f.ContextMenu.MenuItems
For Each mi2 In mi.MenuItems

Next
Next

I get an Object reference not set to an instance of an
object. The ContextMenu = nothing...
What am i doing wrong? It works fine with MainMenus.

Thanks!

Kind Regards
Jorge Cavalheiro
 
Hi there,

first, you don't say when this occure. Straight away or in the end or....

secondly try to put a try...catch around (or is it "on error <do this>") to
catch the error that is thrown and fetch the error message. it might give
you a clue

just a first hunch is that you run out of the limits somewhere, in the
beginning or at the end of the array.
/anders
 
Hello
This a procedure inside a class and called from the form
load event and its purpose is enable/disable certain menu
item based on user priviledges. My class has 4 methods
BrowsweMenus(form), BrowseButtons(form), ActivateMenus
(form) and ActivateButtons(form). All work just fine
except ContextMenus. Only one of them is associated to
the form.

Kind Regards
Jorge
 
Hi Jorge,

I've tried to create a snippet to give me the error you describe. Not that
I'm a good programmer but I think you have missed to set an actrive
contextmenu, You said there where 2 of them right. I suppose that the app
don't know where to begin since there is multiple choise.

continnue to code a bit to see if I can find a solution

/anders
 
The outer loop (mi) is checking each menu item in the context menu.
If that item (mi) is not a sub-menu, then there will be a Nothing
reference for mi2., as mi has no menu items.

main menus are differnet in that you are iterating (mi) over each of
the menu "headers", and each one will typically have menu items in it.
But on a context menu, unless you're using sub-menus, none of the
mi's will have menu items.

Hope that helps,

Kevin
 
Back
Top