CONTEXT MENU WEIRD BEHAVIOUR

There is a weird behaviour in the context menu.

PROBLEM:
      In windows forms application
         add a context menu.
         add a menuitem called samp
         add events Select and PopUp for menuitem samp.
      In Select event of menuitem samp
         add a new menuitem say "Test1"
      
      When you run the application and when your mouse is over the menuitem samp then TEST1 is displayed as the child item.

Now In select event of menuitem samp
 add a new menuitem say "Test1"
 add a new menuitem say "Test2"

When you run the application on mouse over of the menuitem samp no menuitem is displayed.

SOLUTION:
   Could someone help me on how to solve this problem
   I am able to add menuitems to samp in the form load event and also context menu onpopup event.

Thanks in Advance

Regards
Aravind Biran

   




Answer this question

CONTEXT MENU WEIRD BEHAVIOUR

  • Gwen T

    I believe that you are trying to dynamically build a context menu through the events.  I believe the Select event is too late in the process to try and do this.  You could try to do it when the Popup event occurs.  In menus the Popup event is called prior to displaying the menu.  This is where (at least in the Win32 world) you would add child elements.  Select is only called when your menu item is selected and is more for status bar hints than anything.  Furthermore if your menu has submenu items then you'll want the little arrow to appear next to your menu item but this won't occur if the items aren't already added when the menu is displayed.

    I don't understand why you would want to build the menu on the fly anyway.  This seems to be more difficult than it is worth.  Most apps dynamically generate the context menu and all its children at one time and then let Windows handle the rest.  If you are dealing with a really large context menu then I'd say that you might want to reconsider your approach because context menus shouldn't be that big or complex as it sort of defeats the purpose.

    Therefore my suggestion is to build the context menu and its children at one time.  This can occur either when a certain event occurs (such as when a node is selected in a tree) or prior to displaying the context menu itself.  Attempting to dynamically build a context menu seems a little too complex for little gain but if you want to do that then you should build your child menus in the Popup event of the parent.

    Michael Taylor - 11/21/05

  • CONTEXT MENU WEIRD BEHAVIOUR