ContextMenu cm = new ContextMenu(); MenuItem menuItem = new MenuItem("Cut"); menuItem.Click += new EventHandler(Cut_Click); cm.MenuItems.Add(menuItem); private void Cut_Click(object sender, EventArgs e) { // code to perform the Cut action }
MenuItem menuItem = new MenuItem("File"); MenuItem subMenuItem = new MenuItem("New"); subMenuItem.Click += new EventHandler(New_Click); menuItem.MenuItems.Add(subMenuItem); private void New_Click(object sender, EventArgs e) { // code to perform the New action }In this example, we create a new menu item with the label "File". We then create a sub-menu item with the label "New" and add a click event handler to it. We finally add the sub-menu item to the "File" menu item using the MenuItems property. Package Library: System.Windows.Forms.