Solved

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

Posted on 2011-10-01
4
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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