Dynamic Context Menus

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eddie de Bear

    #1

    Dynamic Context Menus

    Hi,

    A project I am working on has a requirement for dynamic
    menus. For the most part this works really well.

    The menus I'm creating a based on files and directories,
    so naturally the menu creation takes some time.

    The approach I took was to override the OnSelect method
    of the MenuItem class, which had code to populate the
    child items.

    This worked really well until I had to use the menu as a
    context menu.. I found that the context menu does not
    refresh properly below the first level menu..

    The only way I can get this to work correctly is to use
    reflection to call the private UpdateMenuItem method on
    the MenuItem class.

    Has anybody else had experiance with this??

    Is the fact that the MenuItem does not refresh correctly
    when in a context menu a bug???

    Any feedback would be great..

    Cheers
  • Mick Doherty

    #2
    Re: Dynamic Context Menus

    I just took a look at that and it does appear to be a serious BUG.
    A workaround, other than your own, is to set the forms ContextMenu property
    to the context menu in question. Of course this is not the ideal situation
    as your context menu will now popup from every control that does not have
    it's own context menu.

    As for that slow menu generation, are you adding the MenuItems to your
    ContextMenu in a loop? If so, a faster way is to create a MenuItem Array and
    set each member of the array to a new menuitem from within the loop. Then
    outside the loop use AddRange to add all the menu items at once.

    "Eddie de Bear" <anonymous@disc ussions.microso ft.com> wrote in message
    news:072701c3a3 3b$281f8c60$a10 1280a@phx.gbl.. .[color=blue]
    > Hi,
    >
    > A project I am working on has a requirement for dynamic
    > menus. For the most part this works really well.
    >
    > The menus I'm creating a based on files and directories,
    > so naturally the menu creation takes some time.
    >
    > The approach I took was to override the OnSelect method
    > of the MenuItem class, which had code to populate the
    > child items.
    >
    > This worked really well until I had to use the menu as a
    > context menu.. I found that the context menu does not
    > refresh properly below the first level menu..
    >
    > The only way I can get this to work correctly is to use
    > reflection to call the private UpdateMenuItem method on
    > the MenuItem class.
    >
    > Has anybody else had experiance with this??
    >
    > Is the fact that the MenuItem does not refresh correctly
    > when in a context menu a bug???
    >
    > Any feedback would be great..
    >
    > Cheers[/color]


    Comment

    • Eddie de Bear

      #3
      Re: Dynamic Context Menus

      The reason me menu creation is slow is because the menu
      is based on existance of files in the OS as well as
      security settings in the database... This retrieval takes
      time.. Not the actual menuitem creation itself..

      I was actually hoping some of the MS guys could possibly
      take a look at this problem, becuase I have spent several
      days on this, and have come to the conclusion that it is
      a bug...

      [color=blue]
      >-----Original Message-----
      >I just took a look at that and it does appear to be a[/color]
      serious BUG.[color=blue]
      >A workaround, other than your own, is to set the forms[/color]
      ContextMenu property[color=blue]
      >to the context menu in question. Of course this is not[/color]
      the ideal situation[color=blue]
      >as your context menu will now popup from every control[/color]
      that does not have[color=blue]
      >it's own context menu.
      >
      >As for that slow menu generation, are you adding the[/color]
      MenuItems to your[color=blue]
      >ContextMenu in a loop? If so, a faster way is to create[/color]
      a MenuItem Array and[color=blue]
      >set each member of the array to a new menuitem from[/color]
      within the loop. Then[color=blue]
      >outside the loop use AddRange to add all the menu items[/color]
      at once.[color=blue]
      >
      >"Eddie de Bear" <anonymous@disc ussions.microso ft.com>[/color]
      wrote in message[color=blue]
      >news:072701c3a 33b$281f8c60$a1 01280a@phx.gbl. ..[color=green]
      >> Hi,
      >>
      >> A project I am working on has a requirement for dynamic
      >> menus. For the most part this works really well.
      >>
      >> The menus I'm creating a based on files and[/color][/color]
      directories,[color=blue][color=green]
      >> so naturally the menu creation takes some time.
      >>
      >> The approach I took was to override the OnSelect method
      >> of the MenuItem class, which had code to populate the
      >> child items.
      >>
      >> This worked really well until I had to use the menu as[/color][/color]
      a[color=blue][color=green]
      >> context menu.. I found that the context menu does not
      >> refresh properly below the first level menu..
      >>
      >> The only way I can get this to work correctly is to use
      >> reflection to call the private UpdateMenuItem method on
      >> the MenuItem class.
      >>
      >> Has anybody else had experiance with this??
      >>
      >> Is the fact that the MenuItem does not refresh[/color][/color]
      correctly[color=blue][color=green]
      >> when in a context menu a bug???
      >>
      >> Any feedback would be great..
      >>
      >> Cheers[/color]
      >
      >
      >.
      >[/color]

      Comment

      • Mick Doherty

        #4
        Re: Dynamic Context Menus

        Well this was really bugging me and I just couldn't let it go.
        After many unsuccessful overcomplicated attempts I finally came up with a
        very simple solution.

        Remove the MenuItem that you wish to add items to, add the items then re-add
        the Menuitem to the ContextMenu.

        VB.Net Example

        Dim DynamicParentIn dex As Integer = DynamicParent.I ndex
        MyContextMenu.M enuItems.Remove (DynamicParent)
        DynamicParent.M enuItems.Clear 'Optional
        DynamicParent.M enuItems.AddRan ge(DynamicMenuI temsArray)
        MyContextMenu.M enuItems.Add(Dy namicParentInde x, DynamicParent)


        "Eddie de Bear" <anonymous@disc ussions.microso ft.com> wrote in message
        news:072701c3a3 3b$281f8c60$a10 1280a@phx.gbl.. .[color=blue]
        > Hi,
        >
        > A project I am working on has a requirement for dynamic
        > menus. For the most part this works really well.
        >
        > The menus I'm creating a based on files and directories,
        > so naturally the menu creation takes some time.
        >
        > The approach I took was to override the OnSelect method
        > of the MenuItem class, which had code to populate the
        > child items.
        >
        > This worked really well until I had to use the menu as a
        > context menu.. I found that the context menu does not
        > refresh properly below the first level menu..
        >
        > The only way I can get this to work correctly is to use
        > reflection to call the private UpdateMenuItem method on
        > the MenuItem class.
        >
        > Has anybody else had experiance with this??
        >
        > Is the fact that the MenuItem does not refresh correctly
        > when in a context menu a bug???
        >
        > Any feedback would be great..
        >
        > Cheers[/color]


        Comment

        Working...