Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Context Menus 101

I have added a Context Menu (popup) to a Windows Form.

At runtime....when I right-click on the associated control....the popup menu activates and shows "Add"

But when I click on Add...nothing happens because I have not associated an event handler for the click on Add.

How do I do this?
ASKER CERTIFIED SOLUTION
Avatar of Timbo87
Timbo87

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Knowlton

ASKER

I am actually adding the pop-up menu items at runtim using the ContextMenu popup event:

private void contextMenuTreeMod_Popup(object sender, System.EventArgs e)
            {
                  // Define the MenuItem objects to display for the TextBox.
                  MenuItem menuItem1 = new MenuItem("&Add");                  
                  
                  // Clear all previously added MenuItems.
                  contextMenuTreeMod.MenuItems.Clear();
 
                  if(contextMenuTreeMod.SourceControl == treeViewInventory)
                  {
                        // Add MenuItems to display for the TextBox.
                        contextMenuTreeMod.MenuItems.Add(menuItem1);                        
                  }
            }      





My intent is for THIS method to fire when the user clicks on ADD..........but the method is not WIRED to the CLICK EVENT

            protected void menuItem1_Click(System.Object sender, System.EventArgs e)
            {
                  // Insert code to handle Click event.
                  MessageBox.Show("Add something to");
            }
Ohhhhhh...........I get it.....duh...............


This works:


            private void contextMenuTreeMod_Popup(object sender, System.EventArgs e)
            {
                  // Define the MenuItem objects to display for the TextBox.
                  MenuItem menuItem1 = new MenuItem("&Add");                  

                  menuItem1.Click += new EventHandler(menuItem1_Click);    ////////////////////////////////////  JUST NEEDED TO ADD THIS PART......AS YOU SUGGESTED.....////////////////////////
                  
                  // Clear all previously added MenuItems.
                  contextMenuTreeMod.MenuItems.Clear();
 
                  if(contextMenuTreeMod.SourceControl == treeViewInventory)
                  {
                        // Add MenuItems to display for the TextBox.
                        contextMenuTreeMod.MenuItems.Add(menuItem1);                        
                  }
            }      

            protected void menuItem1_Click(System.Object sender, System.EventArgs e)
            {
                  // Insert code to handle Click event.
                  MessageBox.Show("Add something to");
            }
            


Let me ask you this:


How would I   UNWIRE     the ADD click event from one method............. and then REWIRE the ADD click event to some other method:


My best guess:

  private void contextMenuTreeMod_Popup(object sender, System.EventArgs e)
          {
               // Define the MenuItem objects to display for the TextBox.
               MenuItem menuItem1 = new MenuItem("&Add");              

               menuItem1.Click += new EventHandler(menuItem1_Click);    ////////////////////////////////////  JUST NEEDED TO ADD THIS PART......AS YOU SUGGESTED.....////////////////////////


           


             //CAN I USE     -=      to     unassign the event handler?????????????????





               
               // Clear all previously added MenuItems.
               contextMenuTreeMod.MenuItems.Clear();
 
               if(contextMenuTreeMod.SourceControl == treeViewInventory)
               {
                    // Add MenuItems to display for the TextBox.
                    contextMenuTreeMod.MenuItems.Add(menuItem1);                    
               }
          }    

          protected void menuItem1_Click(System.Object sender, System.EventArgs e)
          {
               // Insert code to handle Click event.
               MessageBox.Show("Add something to");
          }
         
          protected void menuItem1Alt_Click(System.Object sender, System.EventArgs e)
          {
               // Insert code to handle Click event.
               MessageBox.Show("ALTERNATE Add something to");
          }
         
Avatar of Timbo87
Timbo87

Yes, you can use -= to remove delegates. You can also wire up multiple event handlers if you need to.
wire-up multiple event handlers?

you mean several differnt event handlers for one click....and the one that gets used depends on something?

can you explain?
Multicasting is the ability to call multiple methods with a single delegate.

EventHandler a = new EventHandler(method1);
EventHandler b = new EventHandler(method2);
EventHandler c = new EventHandler(method3);

menuItem.Click = a + b + c;

Then you can add or remove them with += or -=.
Interesting.