Loop Thru Menu Items on main MDI form 
Author Message
 Loop Thru Menu Items on main MDI form

I need to loop thru ALL of my main menu items - even nested ones (i.e. menus
within menus within menus). I need to do this because I enable/disable
certain menu items based upon the user id selected and what permissions they
have available to them (which are read in from a database).  I therefore
need to go thru all the menu items and determine if they need to be
enabled/disabled.

What is the best wayt o do this?  Thanks in advance.

Tom



Sat, 22 Oct 2005 23:25:29 GMT  
 Loop Thru Menu Items on main MDI form

Quote:

> I need to loop thru ALL of my main menu items - even nested ones (i.e. menus
> within menus within menus). I need to do this because I enable/disable
> certain menu items based upon the user id selected and what permissions they
> have available to them (which are read in from a database).  I therefore
> need to go thru all the menu items and determine if they need to be
> enabled/disabled.

> What is the best wayt o do this?  Thanks in advance.

> Tom

Tom,

Make a recursive function (a function that calls it's self) that reads the sub menus. You just need a seed point (your MainMenu) and then pass it to the recursive function:

Private Sub Form_Load()
    Call Par{*filter*}u( me.MainMenu1 )
End Sub

Sub Par{*filter*}u(Parent as MenuItem)
    Dim mnuChild as MenuItem

    For Each mnuChild in Parent.MenuItems
        '// do whatever it is that you want
        '// to do with the menu item here

        '// repeat
        Call Par{*filter*}u( mnuChild )
    Next
End Sub

HTH,
Jeremy



Sun, 23 Oct 2005 01:53:13 GMT  
 Loop Thru Menu Items on main MDI form
Thanks, Jeremy.... I was thinking of doing this (using recursion) but I
didn't know if there was an easier way.  I may not understand it fully, but
can't one use the 'MenuItemCollection' somehow to simply go thru all the
menu items?

Tom



Quote:
> I need to loop thru ALL of my main menu items - even nested ones (i.e.
menus
> within menus within menus). I need to do this because I enable/disable
> certain menu items based upon the user id selected and what permissions
they
> have available to them (which are read in from a database).  I therefore
> need to go thru all the menu items and determine if they need to be
> enabled/disabled.

> What is the best wayt o do this?  Thanks in advance.

> Tom

Tom,

Make a recursive function (a function that calls it's self) that reads the
sub menus. You just need a seed point (your MainMenu) and then pass it to
the recursive function:

Private Sub Form_Load()
    Call Par{*filter*}u( me.MainMenu1 )
End Sub

Sub Par{*filter*}u(Parent as MenuItem)
    Dim mnuChild as MenuItem

    For Each mnuChild in Parent.MenuItems
        '// do whatever it is that you want
        '// to do with the menu item here

        '// repeat
        Call Par{*filter*}u( mnuChild )
    Next
End Sub

HTH,
Jeremy



Sun, 23 Oct 2005 02:06:09 GMT  
 Loop Thru Menu Items on main MDI form
The code that I posted does use *a* MenuItemCollection. When you see "MenuItemCollection" listed in the Intellisense, what you are looking at is the Class Definition, not an accessible instance. In other words, MenuItems is an instance of MenuItemCollection, and MenuItemCollection is a type, not a member variable, property etc...

There is no complete set of menu items accessible from one single collection, they are nested via the MenuItems property (as far as I know).

HTH,
Jeremy

Quote:

> Thanks, Jeremy.... I was thinking of doing this (using recursion) but I
> didn't know if there was an easier way.  I may not understand it fully, but
> can't one use the 'MenuItemCollection' somehow to simply go thru all the
> menu items?

> Tom





> > I need to loop thru ALL of my main menu items - even nested ones (i.e.
> menus
> > within menus within menus). I need to do this because I enable/disable
> > certain menu items based upon the user id selected and what permissions
> they
> > have available to them (which are read in from a database).  I therefore
> > need to go thru all the menu items and determine if they need to be
> > enabled/disabled.

> > What is the best wayt o do this?  Thanks in advance.

> > Tom

> Tom,

> Make a recursive function (a function that calls it's self) that reads the
> sub menus. You just need a seed point (your MainMenu) and then pass it to
> the recursive function:

> Private Sub Form_Load()
>     Call Par{*filter*}u( me.MainMenu1 )
> End Sub

> Sub Par{*filter*}u(Parent as MenuItem)
>     Dim mnuChild as MenuItem

>     For Each mnuChild in Parent.MenuItems
>         '// do whatever it is that you want
>         '// to do with the menu item here

>         '// repeat
>         Call Par{*filter*}u( mnuChild )
>     Next
> End Sub

> HTH,
> Jeremy



Sun, 23 Oct 2005 02:11:40 GMT  
 Loop Thru Menu Items on main MDI form
Cool, now I get it.

And I got it working, but I had to change your code as follows:
-------------
Private Sub Form_Load()
    dim mn as MenuItem
    for each mn in me.MainMenu1.MenuItems
        Call Par{*filter*}u( mn )
    next
End Sub

Sub Par{*filter*}u(Parent as MenuItem)
    Dim mnuChild as MenuItem

    For Each mnuChild in Parent.MenuItems
        '// do whatever it is that you want
        '// to do with the menu item here

        '// repeat
        Call Par{*filter*}u( mnuChild )
    Next
End Sub
-----------
Basically, the recursive routine had to have a MenuItem passed to it;
therefore, I loop thru the main top-level menus, passing each one on to the
recursive routine, which handles everything from there.

Tom


The code that I posted does use *a* MenuItemCollection. When you see
"MenuItemCollection" listed in the Intellisense, what you are looking at is
the Class Definition, not an accessible instance. In other words, MenuItems
is an instance of MenuItemCollection, and MenuItemCollection is a type, not
a member variable, property etc...

There is no complete set of menu items accessible from one single
collection, they are nested via the MenuItems property (as far as I know).

HTH,
Jeremy


Quote:
> Thanks, Jeremy.... I was thinking of doing this (using recursion) but I
> didn't know if there was an easier way.  I may not understand it fully,
but
> can't one use the 'MenuItemCollection' somehow to simply go thru all the
> menu items?

> Tom





> > I need to loop thru ALL of my main menu items - even nested ones (i.e.
> menus
> > within menus within menus). I need to do this because I enable/disable
> > certain menu items based upon the user id selected and what permissions
> they
> > have available to them (which are read in from a database).  I therefore
> > need to go thru all the menu items and determine if they need to be
> > enabled/disabled.

> > What is the best wayt o do this?  Thanks in advance.

> > Tom

> Tom,

> Make a recursive function (a function that calls it's self) that reads the
> sub menus. You just need a seed point (your MainMenu) and then pass it to
> the recursive function:

> Private Sub Form_Load()
>     Call Par{*filter*}u( me.MainMenu1 )
> End Sub

> Sub Par{*filter*}u(Parent as MenuItem)
>     Dim mnuChild as MenuItem

>     For Each mnuChild in Parent.MenuItems
>         '// do whatever it is that you want
>         '// to do with the menu item here

>         '// repeat
>         Call Par{*filter*}u( mnuChild )
>     Next
> End Sub

> HTH,
> Jeremy



Sun, 23 Oct 2005 02:27:41 GMT  
 Loop Thru Menu Items on main MDI form
Good deal, one question though:

How is this (prime read in form_load):

Quote:
>     dim mn as MenuItem
>     for each mn in me.MainMenu1.MenuItems
>         Call Par{*filter*}u( mn )
>     next

different from this (recursive function, comments removed)

Quote:
>     Dim mnuChild as MenuItem
>     For Each mnuChild in Parent.MenuItems
>         Call Par{*filter*}u( mnuChild )
>     Next

Or is it that you don't want it to process the top-level menus the same as lower level menus?

Jeremy

Quote:

> Cool, now I get it.

> And I got it working, but I had to change your code as follows:
> -------------
> Private Sub Form_Load()
>     dim mn as MenuItem
>     for each mn in me.MainMenu1.MenuItems
>         Call Par{*filter*}u( mn )
>     next
> End Sub

> Sub Par{*filter*}u(Parent as MenuItem)
>     Dim mnuChild as MenuItem

>     For Each mnuChild in Parent.MenuItems
>         '// do whatever it is that you want
>         '// to do with the menu item here

>         '// repeat
>         Call Par{*filter*}u( mnuChild )
>     Next
> End Sub
> -----------
> Basically, the recursive routine had to have a MenuItem passed to it;
> therefore, I loop thru the main top-level menus, passing each one on to the
> recursive routine, which handles everything from there.

> Tom



> The code that I posted does use *a* MenuItemCollection. When you see
> "MenuItemCollection" listed in the Intellisense, what you are looking at is
> the Class Definition, not an accessible instance. In other words, MenuItems
> is an instance of MenuItemCollection, and MenuItemCollection is a type, not
> a member variable, property etc...

> There is no complete set of menu items accessible from one single
> collection, they are nested via the MenuItems property (as far as I know).

> HTH,
> Jeremy



> > Thanks, Jeremy.... I was thinking of doing this (using recursion) but I
> > didn't know if there was an easier way.  I may not understand it fully,
> but
> > can't one use the 'MenuItemCollection' somehow to simply go thru all the
> > menu items?

> > Tom





> > > I need to loop thru ALL of my main menu items - even nested ones (i.e.
> > menus
> > > within menus within menus). I need to do this because I enable/disable
> > > certain menu items based upon the user id selected and what permissions
> > they
> > > have available to them (which are read in from a database).  I therefore
> > > need to go thru all the menu items and determine if they need to be
> > > enabled/disabled.

> > > What is the best wayt o do this?  Thanks in advance.

> > > Tom

> > Tom,

> > Make a recursive function (a function that calls it's self) that reads the
> > sub menus. You just need a seed point (your MainMenu) and then pass it to
> > the recursive function:

> > Private Sub Form_Load()
> >     Call Par{*filter*}u( me.MainMenu1 )
> > End Sub

> > Sub Par{*filter*}u(Parent as MenuItem)
> >     Dim mnuChild as MenuItem

> >     For Each mnuChild in Parent.MenuItems
> >         '// do whatever it is that you want
> >         '// to do with the menu item here

> >         '// repeat
> >         Call Par{*filter*}u( mnuChild )
> >     Next
> > End Sub

> > HTH,
> > Jeremy



Sun, 23 Oct 2005 02:37:57 GMT  
 Loop Thru Menu Items on main MDI form
Hmm... I guess I am somewhat confused (dang, menus are much more powerful in
VB.NET as compared to VB6, but they are somewhat more confusing! <grin)

Assuming you have a main menu called MainMenu1, my problem was with your
original line 'Call Par{*filter*}u(me.MainMenu1)' - in the Par{*filter*}u sub it is
expecting a MENUITEM as the paramenter, but in this case you are attempting
to pass it a full MENU item, and it was giving a '... cannot be converted to
...' error message.  Of course the Par{*filter*}u routine needs to have the
parameter as MenuItem since that is what is being passed recursivly back to
it.  That is why I looped thru the main menu items in the Form_Load.  But
yes - I DO want the top-level menu items processed in the Par{*filter*}u routine
also just like all the other menu items. And I can see a problem with that
in my code - the very top level items are not going to be processed....

I must be missing something or my brain is dead today....

Tom


Good deal, one question though:

How is this (prime read in form_load):

Quote:
>     dim mn as MenuItem
>     for each mn in me.MainMenu1.MenuItems
>         Call Par{*filter*}u( mn )
>     next

different from this (recursive function, comments removed)

Quote:
>     Dim mnuChild as MenuItem
>     For Each mnuChild in Parent.MenuItems
>         Call Par{*filter*}u( mnuChild )
>     Next

Or is it that you don't want it to process the top-level menus the same as
lower level menus?

Jeremy


Quote:
> Cool, now I get it.

> And I got it working, but I had to change your code as follows:
> -------------
> Private Sub Form_Load()
>     dim mn as MenuItem
>     for each mn in me.MainMenu1.MenuItems
>         Call Par{*filter*}u( mn )
>     next
> End Sub

> Sub Par{*filter*}u(Parent as MenuItem)
>     Dim mnuChild as MenuItem

>     For Each mnuChild in Parent.MenuItems
>         '// do whatever it is that you want
>         '// to do with the menu item here

>         '// repeat
>         Call Par{*filter*}u( mnuChild )
>     Next
> End Sub
> -----------
> Basically, the recursive routine had to have a MenuItem passed to it;
> therefore, I loop thru the main top-level menus, passing each one on to
the
> recursive routine, which handles everything from there.

> Tom



> The code that I posted does use *a* MenuItemCollection. When you see
> "MenuItemCollection" listed in the Intellisense, what you are looking at
is
> the Class Definition, not an accessible instance. In other words,
MenuItems
> is an instance of MenuItemCollection, and MenuItemCollection is a type,
not
> a member variable, property etc...

> There is no complete set of menu items accessible from one single
> collection, they are nested via the MenuItems property (as far as I know).

> HTH,
> Jeremy



> > Thanks, Jeremy.... I was thinking of doing this (using recursion) but I
> > didn't know if there was an easier way.  I may not understand it fully,
> but
> > can't one use the 'MenuItemCollection' somehow to simply go thru all the
> > menu items?

> > Tom





> > > I need to loop thru ALL of my main menu items - even nested ones (i.e.
> > menus
> > > within menus within menus). I need to do this because I enable/disable
> > > certain menu items based upon the user id selected and what
permissions
> > they
> > > have available to them (which are read in from a database).  I
therefore
> > > need to go thru all the menu items and determine if they need to be
> > > enabled/disabled.

> > > What is the best wayt o do this?  Thanks in advance.

> > > Tom

> > Tom,

> > Make a recursive function (a function that calls it's self) that reads
the
> > sub menus. You just need a seed point (your MainMenu) and then pass it
to
> > the recursive function:

> > Private Sub Form_Load()
> >     Call Par{*filter*}u( me.MainMenu1 )
> > End Sub

> > Sub Par{*filter*}u(Parent as MenuItem)
> >     Dim mnuChild as MenuItem

> >     For Each mnuChild in Parent.MenuItems
> >         '// do whatever it is that you want
> >         '// to do with the menu item here

> >         '// repeat
> >         Call Par{*filter*}u( mnuChild )
> >     Next
> > End Sub

> > HTH,
> > Jeremy



Sun, 23 Oct 2005 03:00:23 GMT  
 Loop Thru Menu Items on main MDI form
:)

My mistake.  The definition for Par{*filter*}u should look like this:

Public Sub Par{*filter*}u( parent as Menu )
...
End Sub

Notice: parent as Menu, not MenuItem

If you look at the .NET Framework Documentation you will see that MainMenu and MenuItem both inherit from Menu, and Menu has a MenuItems property... so the right parameter type is Menu, not MainMenu or MenuItem. This way you can pass in both MainMenus, MenuItems, and you should be able to start it like this now:

Sub Form_Load ( )
    Par{*filter*}u( MainMenu )
End Sub

Jeremy

Quote:

> Hmm... I guess I am somewhat confused (dang, menus are much more powerful in
> VB.NET as compared to VB6, but they are somewhat more confusing! <grin)

> Assuming you have a main menu called MainMenu1, my problem was with your
> original line 'Call Par{*filter*}u(me.MainMenu1)' - in the Par{*filter*}u sub it is
> expecting a MENUITEM as the paramenter, but in this case you are attempting
> to pass it a full MENU item, and it was giving a '... cannot be converted to
> ...' error message.  Of course the Par{*filter*}u routine needs to have the
> parameter as MenuItem since that is what is being passed recursivly back to
> it.  That is why I looped thru the main menu items in the Form_Load.  But
> yes - I DO want the top-level menu items processed in the Par{*filter*}u routine
> also just like all the other menu items. And I can see a problem with that
> in my code - the very top level items are not going to be processed....

> I must be missing something or my brain is dead today....

> Tom



> Good deal, one question though:

> How is this (prime read in form_load):

> >     dim mn as MenuItem
> >     for each mn in me.MainMenu1.MenuItems
> >         Call Par{*filter*}u( mn )
> >     next

> different from this (recursive function, comments removed)

> >     Dim mnuChild as MenuItem
> >     For Each mnuChild in Parent.MenuItems
> >         Call Par{*filter*}u( mnuChild )
> >     Next

> Or is it that you don't want it to process the top-level menus the same as
> lower level menus?

> Jeremy



> > Cool, now I get it.

> > And I got it working, but I had to change your code as follows:
> > -------------
> > Private Sub Form_Load()
> >     dim mn as MenuItem
> >     for each mn in me.MainMenu1.MenuItems
> >         Call Par{*filter*}u( mn )
> >     next
> > End Sub

> > Sub Par{*filter*}u(Parent as MenuItem)
> >     Dim mnuChild as MenuItem

> >     For Each mnuChild in Parent.MenuItems
> >         '// do whatever it is that you want
> >         '// to do with the menu item here

> >         '// repeat
> >         Call Par{*filter*}u( mnuChild )
> >     Next
> > End Sub
> > -----------
> > Basically, the recursive routine had to have a MenuItem passed to it;
> > therefore, I loop thru the main top-level menus, passing each one on to
> the
> > recursive routine, which handles everything from there.

> > Tom



> > The code that I posted does use *a* MenuItemCollection. When you see
> > "MenuItemCollection" listed in the Intellisense, what you are looking at
> is
> > the Class Definition, not an accessible instance. In other words,
> MenuItems
> > is an instance of MenuItemCollection, and MenuItemCollection is a type,
> not
> > a member variable, property etc...

> > There is no complete set of menu items accessible from one single
> > collection, they are nested via the MenuItems property (as far as I know).

> > HTH,
> > Jeremy



> > > Thanks, Jeremy.... I was thinking of doing this (using recursion) but I
> > > didn't know if there was an easier way.  I may not understand it fully,
> > but
> > > can't one use the 'MenuItemCollection' somehow to simply go thru all the
> > > menu items?

> > > Tom





> > > > I need to loop thru ALL of my main menu items - even nested ones (i.e.
> > > menus
> > > > within menus within menus). I need to do this because I enable/disable
> > > > certain menu items based upon the user id selected and what
> permissions
> > > they
> > > > have available to them (which are read in from a database).  I
> therefore
> > > > need to go thru all the menu items and determine if they need to be
> > > > enabled/disabled.

> > > > What is the best wayt o do this?  Thanks in advance.

> > > > Tom

> > > Tom,

> > > Make a recursive function (a function that calls it's self) that reads
> the
> > > sub menus. You just need a seed point (your MainMenu) and then pass it
> to
> > > the recursive function:

> > > Private Sub Form_Load()
> > >     Call Par{*filter*}u( me.MainMenu1 )
> > > End Sub

> > > Sub Par{*filter*}u(Parent as MenuItem)
> > >     Dim mnuChild as MenuItem

> > >     For Each mnuChild in Parent.MenuItems
> > >         '// do whatever it is that you want
> > >         '// to do with the menu item here

> > >         '// repeat
> > >         Call Par{*filter*}u( mnuChild )
> > >     Next
> > > End Sub

> > > HTH,
> > > Jeremy



Sun, 23 Oct 2005 03:38:40 GMT  
 Loop Thru Menu Items on main MDI form
Cool, thanks Jeremy, that did the trick.

Tom


:)

My mistake.  The definition for Par{*filter*}u should look like this:

Public Sub Par{*filter*}u( parent as Menu )
...
End Sub

Notice: parent as Menu, not MenuItem

If you look at the .NET Framework Documentation you will see that MainMenu
and MenuItem both inherit from Menu, and Menu has a MenuItems property... so
the right parameter type is Menu, not MainMenu or MenuItem. This way you can
pass in both MainMenus, MenuItems, and you should be able to start it like
this now:

Sub Form_Load ( )
    Par{*filter*}u( MainMenu )
End Sub

Jeremy


Quote:
> Hmm... I guess I am somewhat confused (dang, menus are much more powerful
in
> VB.NET as compared to VB6, but they are somewhat more confusing! <grin)

> Assuming you have a main menu called MainMenu1, my problem was with your
> original line 'Call Par{*filter*}u(me.MainMenu1)' - in the Par{*filter*}u sub it is
> expecting a MENUITEM as the paramenter, but in this case you are
attempting
> to pass it a full MENU item, and it was giving a '... cannot be converted
to
> ...' error message.  Of course the Par{*filter*}u routine needs to have the
> parameter as MenuItem since that is what is being passed recursivly back
to
> it.  That is why I looped thru the main menu items in the Form_Load.  But
> yes - I DO want the top-level menu items processed in the Par{*filter*}u
routine
> also just like all the other menu items. And I can see a problem with that
> in my code - the very top level items are not going to be processed....

> I must be missing something or my brain is dead today....

> Tom



> Good deal, one question though:

> How is this (prime read in form_load):

> >     dim mn as MenuItem
> >     for each mn in me.MainMenu1.MenuItems
> >         Call Par{*filter*}u( mn )
> >     next

> different from this (recursive function, comments removed)

> >     Dim mnuChild as MenuItem
> >     For Each mnuChild in Parent.MenuItems
> >         Call Par{*filter*}u( mnuChild )
> >     Next

> Or is it that you don't want it to process the top-level menus the same as
> lower level menus?

> Jeremy



> > Cool, now I get it.

> > And I got it working, but I had to change your code as follows:
> > -------------
> > Private Sub Form_Load()
> >     dim mn as MenuItem
> >     for each mn in me.MainMenu1.MenuItems
> >         Call Par{*filter*}u( mn )
> >     next
> > End Sub

> > Sub Par{*filter*}u(Parent as MenuItem)
> >     Dim mnuChild as MenuItem

> >     For Each mnuChild in Parent.MenuItems
> >         '// do whatever it is that you want
> >         '// to do with the menu item here

> >         '// repeat
> >         Call Par{*filter*}u( mnuChild )
> >     Next
> > End Sub
> > -----------
> > Basically, the recursive routine had to have a MenuItem passed to it;
> > therefore, I loop thru the main top-level menus, passing each one on to
> the
> > recursive routine, which handles everything from there.

> > Tom



> > The code that I posted does use *a* MenuItemCollection. When you see
> > "MenuItemCollection" listed in the Intellisense, what you are looking at
> is
> > the Class Definition, not an accessible instance. In other words,
> MenuItems
> > is an instance of MenuItemCollection, and MenuItemCollection is a type,
> not
> > a member variable, property etc...

> > There is no complete set of menu items accessible from one single
> > collection, they are nested via the MenuItems property (as far as I
know).

> > HTH,
> > Jeremy



> > > Thanks, Jeremy.... I was thinking of doing this (using recursion) but
I
> > > didn't know if there was an easier way.  I may not understand it
fully,
> > but
> > > can't one use the 'MenuItemCollection' somehow to simply go thru all
the
> > > menu items?

> > > Tom





> > > > I need to loop thru ALL of my main menu items - even nested ones
(i.e.
> > > menus
> > > > within menus within menus). I need to do this because I
enable/disable
> > > > certain menu items based upon the user id selected and what
> permissions
> > > they
> > > > have available to them (which are read in from a database).  I
> therefore
> > > > need to go thru all the menu items and determine if they need to be
> > > > enabled/disabled.

> > > > What is the best wayt o do this?  Thanks in advance.

> > > > Tom

> > > Tom,

> > > Make a recursive function (a function that calls it's self) that reads
> the
> > > sub menus. You just need a seed point (your MainMenu) and then pass it
> to
> > > the recursive function:

> > > Private Sub Form_Load()
> > >     Call Par{*filter*}u( me.MainMenu1 )
> > > End Sub

> > > Sub Par{*filter*}u(Parent as MenuItem)
> > >     Dim mnuChild as MenuItem

> > >     For Each mnuChild in Parent.MenuItems
> > >         '// do whatever it is that you want
> > >         '// to do with the menu item here

> > >         '// repeat
> > >         Call Par{*filter*}u( mnuChild )
> > >     Next
> > > End Sub

> > > HTH,
> > > Jeremy



Sun, 23 Oct 2005 04:19:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Making Copy Of a MDI Main Form Menu for Child

2. Popup Menu hides Main MDI Menu

3. Add Menu Item to Main Word Menu

4. Creating menu item on the right most portion of main menu

5. MDI form Menu Items

6. MDI Forms and Enabled Menu Items

7. VB Bug? - Menu Items when using MDI Form

8. Looping thru the items

9. ListView How to loop thru all items

10. VB3: modal form gets hidden by main MDI form in Win98

11. icons/bmps onto menu items on a MDI menu

12. How to auto size the main form/ MDI form

 

 
Powered by phpBB® Forum Software