Solved

Context Menus 101

Posted on 2004-10-06
8
275 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:knowlton
  • 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: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: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
 
LVL 5

Author Comment

by: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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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: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:knowlton
ID: 12242800
Interesting.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now