Showing a Context Menu for an item in a ListView

You can trigger MouseDown or MouseUp event of ListView in which if MouseButton.Right then grab the selected Item by using ListView.Hittest and give the Context menu related to that Selected Item.

For more clear info you can go through this link


You are going to have to use the ListViews Context Menu, but change it according to the ListView Item you right click on.

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    bool match = false;

    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        foreach (ListViewItem item in listView1.Items)
        {
            if (item.Bounds.Contains(new Point(e.X, e.Y)))
            {
                MenuItem[] mi = new MenuItem[] { new MenuItem("Hello"), new MenuItem("World"), new MenuItem(item.Name) };
                listView1.ContextMenu = new ContextMenu(mi);
                match = true;
                break;
            }
        }
        if (match)
        {
            listView1.ContextMenu.Show(listView1, new Point(e.X, e.Y));
        }
        else
        {
            //Show listViews context menu
        }

    }

}

Fully solution

  • Pops up when user right click on a item in a listView.
  • Avoid an exception if the list have no items.
  • If an item was selected, display Delete and Edit options.

enter image description here

Code:

private void Form1_Load(object sender, EventArgs e)
{
    listView1.MouseUp += new MouseEventHandler(listView1_MouseClick);

}

private void listView1_MouseClick(object sender, MouseEventArgs e)
{
    string id = "xxx";//extra value

    if (e.Button == MouseButtons.Right)
    {
        if (listView1.FocusedItem != null && listView1.FocusedItem.Bounds.Contains(e.Location) == true)
        {
            ContextMenu m = new ContextMenu();
            MenuItem cashMenuItem = new MenuItem("編輯");
            cashMenuItem.Click += delegate (object sender2, EventArgs e2) {
                ActionClick(sender, e, id);
            };// your action here 
            m.MenuItems.Add(cashMenuItem);

            MenuItem cashMenuItem2 = new MenuItem("-");
            m.MenuItems.Add(cashMenuItem2);

            MenuItem delMenuItem = new MenuItem("刪除");
            delMenuItem.Click += delegate (object sender2, EventArgs e2) {
                DelectAction(sender, e, id);
            };// your action here
            m.MenuItems.Add(delMenuItem);

            m.Show(listView1, new Point(e.X, e.Y));

        }
    }
}

private void DelectAction(object sender, MouseEventArgs e, string id)
{
    ListView ListViewControl = sender as ListView;
    foreach (ListViewItem eachItem in ListViewControl.SelectedItems)
    {
        // you can use this idea to get the ListView header's name is 'Id' before delete
        Console.WriteLine(GetTextByHeaderAndIndex(ListViewControl, "Id", eachItem.Index) );
        ListViewControl.Items.Remove(eachItem);
    }
}

private void ActionClick(object sender, MouseEventArgs e, string id)
{
    //id is extra value when you need or delete it
    ListView ListViewControl = sender as ListView;
    foreach (ListViewItem tmpLstView in ListViewControl.SelectedItems)
    {
        Console.WriteLine(tmpLstView.Text);
    }

}

public static string GetTextByHeaderAndIndex(ListView listViewControl, string headerName, int index)
{
    int headerIndex = -1;
    foreach (ColumnHeader header in listViewControl.Columns)
    {
        if (header.Name == headerName)
        {
            headerIndex = header.Index;
            break;
        }
    }
    if (headerIndex > -1)
    {
        return listViewControl.Items[index].SubItems[headerIndex].Text;
    }
    return null;
}

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

You can put connected client information in the contextMenuStrip1. and when you right click on a item, you can show the information from that contextMenuStrip1.

Tags:

C#

Winforms