0

The title pretty much says it all. Can someone explain to me how can I open a context menu by selecting and then right clicking on a ListViewItem of a ListView?

I tried using the following code

private void listView1_MouseClick(object sender, MouseEventArgs e)
{            
    if (e.Button == MouseButtons.Right)
    {
        if (listView1.FocusedItem.Bounds.Contains(e.Location) == true)
        {
            contextMenuStrip1.Show(Cursor.Position);
        }
    } 
}

But I don't know how to register this event handler with the ListView. Every time I try I get the error that the delegates parameters are wrong because I use MouseEventArgs instead of EventArgs.

This is the wrong code I'm using to register the EventHandler

this.listView1.MouseClick += new System.EventHandler(this.listView1_MouseClick);
Sinatr
  • 20,892
  • 15
  • 90
  • 319
apboutos
  • 109
  • 2
  • 16

1 Answers1

0

bind the contextmenu to the listview using listView1.ContextMenu=contexMenu1

then you can use the below code in listView1.MouseDown

ListViewHitTestInfo lstHitTestInfo = listView1.HitTest(e.X, e.Y);
                if (e.Button == MouseButtons.Right)
                {
                    if (lstHitTestInfo.Item != null)
                    {
                        listView1.ContextMenuStrip = contextMenuStrip1;
                    }
                }

this will select the listviewitem and display the contextmenu.