Solved

Context Menus 101

Posted on 2004-10-06
8
279 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 15

Accepted Solution

by:
Timbo87 earned 500 total points
ID: 12241969
If you're using Visual Studio .NET, just double-click the Add menu option in design mode. Otherwise here is an example with a delegate.

Declaring the Add button:
private MenuItem menuItem;

Hooking up an event handler with a delegate:
menuItem.Click += new EventHandler(menuItem_Click);

The event handler:
private void menuItem_Click(object sender, EventArgs e)
{
     MessageBox.Show("Add");
}

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12242000
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");
            }
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12242036
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");
            }
            


0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12242074
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");
          }
         
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12242258
Yes, you can use -= to remove delegates. You can also wire up multiple event handlers if you need to.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12242297
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?
0
 
LVL 15

Expert Comment

by:Timbo87
ID: 12242391
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 -=.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 12242800
Interesting.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question