ListView MouseClick Event

I have a ListView in which I want to display one context menu if the item is right-clicked and the other if click occurs in the ListView control. The problem I get is that the MouseClick event fires only when I right-click, not in the control. What causes this and how can I get around this?

+3
source share
3 answers

You can subclass ListView to add a right-click event:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace MyCustomControls
{

public delegate void MyDelegate(Object sender, EventArgs e);

class MyListView : ListView
{

    private static readonly object EventRightClickRaised = new object();

    public MyListView() 
    {
        //RightClick += new MyDelegate(OnRightClick);
    }

    public event EventHandler RightClick
    {
        add
        {
            Events.AddHandler(EventRightClickRaised, value);
        }
        remove
        {
            Events.RemoveHandler(EventRightClickRaised, value);
        }
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            OnRightClick(EventArgs.Empty);
        }
        base.OnMouseUp(e);
    }

    protected void OnRightClick(EventArgs e)
    {
        EventHandler RightClickRaised = (EventHandler)Events[EventRightClickRaised];
        if (RightClickRaised != null)
        {
            RightClickRaised(this, e);
        }
    }

}
}
+2
source

Use MouseUp instead of MouseClick! Sorry about that. Must be googled harder.

+3
source

(, ), MouseEnter(). , .

-1
source

Source: https://habr.com/ru/post/1703086/


All Articles