Solved

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

Posted on 2011-10-01
4
249 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

21 Experts available now in Live!

Get 1:1 Help Now