Solved

Show a contextmenu when right-click on a selected node of a lisview.

Posted on 2011-10-01
4
250 Views
Last Modified: 2013-12-16
Dear Experts,

I have set a contextmenu to a listview. But as you can see in the picture
I can right-click anywhere in the listview.

What I would like is to show the contextmenu only when a user right-clicks
on a selected node.

Who knows the answer?

Peter
Example.jpg
0
Comment
Question by:peterkiers
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36896466
I believe you could do something like this:  maintain a reference to the ContextMenu, but initially set the ListView's ContextMenu to null. Then, add a SelectedIndexChanged handler and check which mouse button was pressed--if the right button was pressed, assign the menu to to the ListView's ContextMenu property; otherwise, assign null. To check which button was pressed, you can keep a global MouseButtons variable that you assign in handlers for both the MouseDown and MouseUp events of the ListView.

Here is an example:
using System;
using System.Windows.Forms;

namespace _27375273
{
    public partial class Form1 : Form
    {
        MouseButtons button;
        ContextMenu menu;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            menu = new ContextMenu();
            menu.MenuItems.Add(new MenuItem("Copy", copyMenuItem_Click));
            menu.MenuItems.Add(new MenuItem("Paste", pasteMenuItem_Click));
        }

        private void copyMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Text copied.");
        }

        private void pasteMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Text pasted.");
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (button == MouseButtons.Right)
            {
                this.listView1.ContextMenu = menu;
            }
            else
            {
                this.listView1.ContextMenu = null;
            }
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            button = e.Button;
        }

        private void listView1_MouseUp(object sender, MouseEventArgs e)
        {
            button = MouseButtons.None;
        }
    }
}

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36896486
There's a small bug in the above code which does not clear the menu after a selection has been made and a subsequent non-item click occurs. I'm looking into it.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36896515
Change line 35 in the above to:

if (button == MouseButtons.Right && this.listView1.SelectedIndices.Count > 0)

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 36896820
Thansk.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 45
designing in object programming 12 75
use of unassigned local variable 'id' 6 38
Help with adding DLL file in Windows project 20 28
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now