// Create a ContextMenuStrip instance ContextMenuStrip rightClickMenu = new ContextMenuStrip(); // Add options to the menu rightClickMenu.Items.Add("Option 1"); rightClickMenu.Items.Add("Option 2"); // Assign the menu to a control button1.ContextMenuStrip = rightClickMenu; // Suspend layout of the control button1.SuspendLayout(); // Add more controls to the form // Resume layout of the control after adding new controls button1.ResumeLayout();
// Create a ContextMenuStrip instance ContextMenuStrip rightClickMenu = new ContextMenuStrip(); // Add options to the menu ToolStripMenuItem option1 = new ToolStripMenuItem("Option 1"); ToolStripMenuItem option2 = new ToolStripMenuItem("Option 2"); // Set up click event handlers for the options option1.Click += new EventHandler(Option1_Click); option2.Click += new EventHandler(Option2_Click); // Add the options to the menu rightClickMenu.Items.Add(option1); rightClickMenu.Items.Add(option2); // Assign the menu to a control textBox1.ContextMenuStrip = rightClickMenu; // Method to execute when option1 is clicked private void Option1_Click(Object sender, EventArgs e) { // Do something } // Method to execute when option2 is clicked private void Option2_Click(Object sender, EventArgs e) { // Do something else }In this example, we create a ContextMenuStrip instance, add some options to it using ToolStripMenuItem objects, and set up click event handlers for each option. We then assign the menu to a text box control, and define the methods to execute when each option is clicked. The System.Windows.Forms namespace belongs to the .NET Framework Class Library.