Is there a way to assign ContextMenuStrip to ListViewItem?

I want to assign the same ContextMenuStrip to all ListViewItems in the form. These ListViewItems are created dynamically.

Unfortunately, it looks like ListViewItems does not have a ContextMenuStrip property that can be assigned (of course, ListView itself).

Should I just assign a ContextMenuStrip ListView, and then based on the currently selected ListView?

+3
source share
1 answer
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        var hitTestInfo = listView1.HitTest(e.X, e.Y);
        if (hitTestInfo.Item != null)
        {
            var loc = e.Location;
            loc.Offset(listView1.Location);

            // Adjust context menu (or it contents) based on hitTestInfo details
            this.contextMenuStrip2.Show(this, loc);
        }
    }
}
+5
source

All Articles