This is easy!
But I admit, it can be rather confusing at first if you also take the old style MenuItem stuff into account.
I hope my code is as clear as possible. If not please let me know.
To test it, just start a forms application and paste it just after InitializeComponent();
public partial class Form1 : Form
// menu appears after rightclicking the control, in this case the form control
ContextMenuStrip myMenu = new ContextMenuStrip();
this.ContextMenuStrip = myMenu; //for this form, could be for a button etc.
myMenu.Items.Add("Form Info2"); // add some items to the menu
// create item with submenus Item1 and Item2
ToolStripMenuItem mySubMenu = new ToolStripMenuItem();
mySubMenu.Text = "InfoMenu1";
ToolStripMenuItem myItem = new ToolStripMenuItem();
myItem.Text = "Item1";
myItem.Click += new EventHandler(Item1_Menu_Click);
myItem = new ToolStripMenuItem(); //reuse of name
myItem.Text = "Item2";
// further menu items can be added etc.
//fourth item added so index here is 3
myMenu.Items.Click += new EventHandler(FormCloseMenu_Click);
// Just two eventhandlers added as an example
private void Item1_Menu_Click(object sender, EventArgs e)
private void FormCloseMenu_Click(object sender, EventArgs e)
MessageBox.Show("Form wil be closed");
Are you able to help answer this sponsored question?
Either the question asker or someone else made a financial contribution in order to feature this question and encourage quality replies.