How to Add Right Click Menu to an Item in C#

Muhammad Zeeshan Feb 02, 2024
How to Add Right Click Menu to an Item in C#

This article will discuss adding a right-click menu to an item in a C# windows form.

Use ContextMenuStrip in C#

Follow the below steps to add a ContextMenuStrip into windows form and to add its item.

  • Open Visual Studio and create or open an existing Windows Form project.
  • Go to View > ToolBox and enter ContextMenuStrip in search of ToolBox.
  • Double click on ContextMenuStrip in ToolBox.
  • It will add ContextMenuStrip on your form, as shown below.

    Add Context Menu

  • Now, right-click on the ContextMenuStrip and click on the properties.

    Context Menu Properties

  • In the right-down properties panel, find Items and click on the three-dot option.

    Find Items in properties panel

  • Now, click the Add button to add items, as I added three items below.

    Add Items in the Context Menu

  • Create items Events by double-clicking on each item.

    Create Event

  • Right click on Windows form > properties and set ContextMenuStrip property as contextMenuStrip1.

    Set ContextMenuStrip Properties

  • After creating events where you’ll write your logic, I’ll leave it blank so you can write your code here.
    private void toolStripMenuItem1_Click(object sender, EventArgs e) {}
    private void toolStripMenuItem2_Click(object sender, EventArgs e) {}
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {}
    

Window Form Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RightClickItemByZeeshan {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e) {}

    private void toolStripMenuItem1_Click(object sender, EventArgs e) {}

    private void toolStripMenuItem2_Click(object sender, EventArgs e) {}

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
      Application.Exit();
    }
  }
}

Output:

Add Right Click Menu to an Item

Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Csharp GUI