WinForms C#: How do I associate event with a ToolStripMenuItem ?

Posted on 2012-09-21
Last Modified: 2012-09-22
I can no longer use the Designer to create and populate my ToolStripMenuItems, because my context menus need to be 100% dynamically created.  So I need to do them in code.

How can I associate an existing event handler with a ToolStripMenuItem?

Question by:newbieweb
    LVL 9

    Accepted Solution

    for each menu item you add, set the event handler like this:

    menuItem.Click +=new EventHandler(menuItem_Click);

    //and then put the code you want to execute in the menuItem_Click method
    private void menuItem_Click(object sender, EventArgs e)
              // code here will be executed after clicking on menu item

    Author Closing Comment


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    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!
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 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

    18 Experts available now in Live!

    Get 1:1 Help Now