How to Add Right Click Menu to an Item in C#
Muhammad Zeeshan
Feb 02, 2024
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 enterContextMenuStrip
in search of ToolBox. -
Double click on
ContextMenuStrip
in ToolBox. -
It will add
ContextMenuStrip
on your form, as shown below. -
Now, right-click on the
ContextMenuStrip
and click on the properties. -
In the right-down properties panel, find
Items
and click on the three-dot option. -
Now, click the
Add
button to add items, as I added three items below. -
Create items
Events
by double-clicking on each item. -
Right click on
Windows form
>properties
and setContextMenuStrip
property ascontextMenuStrip1
. -
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:
Author: Muhammad Zeeshan
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