Solved

Context Menus 101

Posted on 2004-10-06
8
276 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

21 Experts available now in Live!

Get 1:1 Help Now