Click here to Skip to main content
13,344,765 members (61,509 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 5 May 2011

How to add ToolStripMenuItems to a MenuStrip or ContextMenu dynamically

, 6 May 2011
Rate this:
Please Sign up or sign in to vote.
Think about the Office Word application's file menustrip. The files which you last open are listed there. How is this possible? Here is the code:

ToolStripMenuItem NEW;
NEW = new ToolStripMenuItem(text);
NEW.Text = text;
NEW.Click += new EventHandler(Item_Click);
NEW.CheckOnClick = true;

Then you can use the Click event like:

private void Item_Click(object sender, EventArgs e)
    if (sender is ToolStripMenuItem)  //Check On Click.
        foreach (ToolStripMenuItem item in (((ToolStripMenuItem)sender).GetCurrentParent().Items))
            if (item == sender)
                txtNoteName.Text = item.Text;
                item.Checked = true;
            if ((item != null) && (item != sender))
                item.Checked = false;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Team Leader Eterne
Turkey Turkey
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralI hope that this article is not finished. It lacks considera... Pin
mmansf7-May-11 6:59
membermmansf7-May-11 6:59 
I hope that this article is not finished. It lacks considerable information...especially on how the menuitem has been stored in the application for future recall. How the list of menuitems is maintained, etc...

Your code, above, is not complete.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 6 May 2011
Article Copyright 2011 by SercanOzdemir
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid