Solved

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

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Thansk.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

18 Experts available now in Live!

Get 1:1 Help Now