CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2003
    Posts
    213

    Context Menu (submenu) question

    Hi,

    I am creating a submenu at the runtime and this submenu can have many items (the number is not known at compile time). I want to how to create event handler for these menuitems. Is there a generalized way to handle the events on these submenu items? Please look at the following code and suggest:

    Code:
    	public void GetAllHierarchyList(MenuItem mnuFile)
    	{
    			
    	mnuFile.MenuItems.Clear();
    	foreach (HierarchyNode h in this.hierarchy.HierarchieNodes)
    	{
    	    mnuFile.MenuItems.Add (h.Type); //want event handler for all these 
    //items
    	}
    			
    	}

  2. #2
    Join Date
    Mar 2003
    Posts
    18

    Re: Context Menu (submenu) question

    After adding the menu item

    Code:
    mnuFile.MenuItems.Add (h.Type);
    add an event handler
    Code:
    mnuFile.MenuItems.Click += new System.EventHandler(MenuItem_Click);
    Add the event handler

    Code:
    private void MenuItem_Click(object sender, EventArgs e)
    {
    // Add code here
    }

  3. #3
    Join Date
    Aug 2003
    Posts
    213

    Re: Context Menu (submenu) question

    I tried your code but it gives error:

    error CS0117: 'System.Windows.Forms.Menu.MenuItemCollection' does not contain a definition for 'Click'

    for the line

    Code:
    mnuFile.MenuItems.Click += new System.EventHandler(this.MenuItemAddExisiting_Click);

  4. #4
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Context Menu (submenu) question

    RaviGubbi had the right idea. As you've found out MenuItemCollection does not understand Click but MenuItem does. So
    Code:
    mnuFile.MenuItems.Add (h.Type, new System.EventHandler(this.MenuItemAddExisting_Click));
    
    OR
    
    MenuItem menuItem = new MenuItem(h.Type);
    menuItem.Click += new System.EventHandler(this.MenuItemAddExisting_Click);
    mnuFile.MenuItems.Add(menuItem);
    The next problem you have is deciding what to do when a menuItem is clicked...

  5. #5
    Join Date
    Aug 2003
    Posts
    213

    Re: Context Menu (submenu) question

    Thanks. Help me out a little more.
    How to find out info about the menuitem that's clicked from the handler/
    Code:
    private void MenuItem_Click(object sender, EventArgs e)
    {
    // Add code here
    }
    Thanks

  6. #6
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Context Menu (submenu) question

    Code:
    MenuItem menuItem = sender as MenuItem;

  7. #7
    Join Date
    Aug 2003
    Posts
    213

    Re: Context Menu (submenu) question

    Thanks dude!

  8. #8
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: Context Menu (submenu) question

    Since I got a thankyou, consider this:
    Code:
    foreach (HierarchyNode node in this.hierarchy.HierarchieNodes)
    {
        // create a handler for each node
        MenuItemHandler handler = new MenuItemHandler(node);
    
        // ...and assign it to a menu item
        MenuItem menuItem = new MenuItem(node.type);
        menuItem.Click += new System.EventHandler(handler.MenuItem_Click);
    
        mnuFile.MenuItems.Add (menuItem); 
    }
    
    private class MenuItemHandler
    {
        public MenuItemHandler(HierarchyNode node)
        {
            // TODO store whatever you need
        }
    
       public void MenuItem_Click(object sender, EventArgs e)
      {
           // Since one-to-one relation ship with menuItem no need to ask who the 
           // sender is.
          DoSomething();
      }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured