56

I have been searching for a while for a simple right-click menu for a single item. For example if I right-click on a picture I want a little menu to come up with my own labels: Add, Remove etc. If anyone could help I would be most greatful.

Thanks for looking.

Here is the completed code:

    ContextMenu cm = new ContextMenu();             
    cm.MenuItems.Add("Item 1", new EventHandler(Removepicture_Click));             
    cm.MenuItems.Add("Item 2", new EventHandler(Addpicture_Click));              
    pictureBox1.ContextMenu = cm; 

5 Answers 5

84

Add a contextmenu to your form and then assign it in the control's properties under ContextMenuStrip.

ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Item 1");
cm.MenuItems.Add("Item 2");
    
pictureBox1.ContextMenu = cm;
5
  • @Marshal I didn't get but a quick search showed the way: stackoverflow.com/questions/5789023/…
    – ErTR
    Jan 26, 2016 at 2:16
  • 1
    how can ı create an event when user click Item1 ? Nov 1, 2019 at 10:15
  • How do I handle the event for the menu item... like know if it's item 1 or item 2?
    – Si8
    Apr 2, 2021 at 16:06
  • to add events you just need to assign new event handler to menuItem dynamically. MenuItem has constructor (string text, EventHandler OnClick) so you can add new menuitem with event like so cm.MenuItems.add(new MenuItem("Item 1", new EventHandler(item1_Click)));
    – LongToeBoy
    Jul 6, 2021 at 17:54
  • As a note, you don't need to add a right-click event to show the ContextMenu - simply setting ContextMenu = cm will automatically add that handling. Oct 20, 2023 at 16:04
43

This is a comprehensive answer to this question. I have done this because this page is high on the Google search results and the answer does not go into enough detail. This post assumes that you are competent at using Visual Studio C# forms. This is based on VS2012.

  1. Start by simply dragging a ContextMenuStrip onto the form. It will just put it into the top left corner where you can add your menu items and rename it as you see fit.

  2. You will have to view code and enter in an event yourself on the form. Create a mouse down event for the item in question and then assign a right click event for it like so (I have called the ContextMenuStrip "rightClickMenuStrip"):

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
    switch (e.Button)
        {
            case MouseButtons.Right:
            {
                rightClickMenuStrip.Show(this, new Point(e.X, e.Y));//places the menu at the pointer position
            }
            break;
        }
    }
    
  3. Assign the event handler manually to the form.designer (you may need to add a "using" for System.Windows.Forms; You can just resolve it):

    this.pictureBox1.MouseDown += new MouseEventHandler(this.pictureBox1_MouseDown);
    
  4. All that is needed at this point is to simply double click each menu item and do the desired operations for each click event in the same way you would for any other button.

This is the basic code for this operation. You can obviously modify it to fit in with your coding practices.

4
  • great answer! You could also have utilized an if statement If( e.Button == MouseButtons.Right)
    – Nate S.
    Jan 26, 2015 at 20:41
  • 10
    Unless something has changed since this answer was posted, you do not need to manually specify a MouseDown event in the code. All you have to do is set the ContextMenuStrip property to the correct ContextMenuStrip for the control you want to associate it with. This cuts out steps 2 and 3 (although adding a much smaller step 2) to your solution. Nov 12, 2015 at 16:56
  • 3
    Still valid in VS2019
    – Cullub
    Jun 10, 2019 at 16:20
  • @BrootsWaymb Maybe you'd like to edit the answer to add that?
    – Cullub
    Jun 10, 2019 at 16:21
21

If you are using Visual Studio, there is a GUI solution as well:

  1. From Toolbox add a ContextMenuStrip
  2. Select the context menu and add the right click items
  3. For each item set the click events to the corresponding functions
  4. Select the form / button / image / etc (any item) that the right click menu will be connected
  5. Set its ContextMenuStrip property to the menu you have created.
3

Having just messed around with this, it's useful to kjnow that the e.X / e.Y points are relative to the control, so if (as I was) you are adding a context menu to a listview or something similar, you will want to adjust it with the form's origin. In the example below I've added 20 to the x/y so that the menu appears slightly to the right and under the cursor.

cmDelete.Show(this, new Point(e.X + ((Control)sender).Left+20, e.Y + ((Control)sender).Top+20));
3

Using visual studio is much easy as Dorku said, however I put step by step more in detail

  • Goto View->ToolBox

  • Enter "Context" in Search ToolBox

  • Double Click over "ContextMenuStrip" on ToolBox

  • Right click "ContextMenuStrip1" on the form go to properties and rename as you want ex: "ContextMenuStripReports"

  • Right click "ContextMenuStripReports" go to properties and click on ellipsis (...button) and add menu items as below

  • toolStripMenuItem1

  • toolStripMenuItem2

  • Open your yourform.Designer.cs search for toolStripMenuItem1 add below
    this.toolStripMenuItem1.Click += ToolStripMenuItem1_Click;

  • Do the same for toolStripMenuItem2

  • Open yourform.cs

    private void ToolStripMenuItem1_Click(object sender, System.EventArgs e)     
    {    }    
    private void ToolStripMenuItem2_Click(object sender, System.EventArgs e)     
    {    }   
    
  • Right click on form-> propertyies
    set ContextMenuStrip property as ContextMenuStripReports

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.