?
Solved

Context Menus 101

Posted on 2004-10-06
8
Medium Priority
?
284 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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