How To Add Items To A Context Menu Programatically (C#)

Posted on 2008-11-11
Last Modified: 2013-12-17
I have a context menu attached to a form with some predtermined things I added during "design time". What I want to do is get a list of all open forms, and put the form titles into a Menu Item (Form List) as sub menu items. This is how I see doing those, though the coding is definitely wrong:

//This part works fine
ToolStripMenuItem contextMenuItem;
contextMenuItem = new ToolStripMenuItem("Form List");
contextMenuItem.Click += new System.EventHandler(this.contextMenuItem_Click);

//This part doesnt
foreach (Form form in Application.Openforms)

So if anyone can show me how to make that second part work I would greatly appreciate it. Thanks.
Question by:Pretzel_Jesus
    LVL 14

    Accepted Solution

    I think it should be something like this:

    foreach (Form form in Application.Openforms)
          ToolStripMenuItem myToolStripMenuItem = new ToolStripMenuItem(form.Text, null, null, form.Text);
    LVL 14

    Expert Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    This article will show, step by step, how to integrate R code into a R Sweave document
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now