0

I have 8 listViews controls in my C# program. I have just one contextMenuStrip that they all reference.

How can I know which listView control triggered the contextMenuStrip selection?

TKO
  • 29
  • 5

2 Answers2

0

Thanks for that, it led me to the solution.

I needed to declare lvX in the class and then assign it in each of the listviews when hovered over.

private void lvStatus_MouseHover(object sender, EventArgs e)
{
  lvX = (ListView)sender; 
}

Then when the contextMenuItem was selected I simply worked with the lvX

foreach (ListViewItem listItem in lvX.Items)
{
  listItem.Checked = true;
} 
TKO
  • 29
  • 5
0

Addition to your solution and Jimi's. Here's another one.

Just handle the Click event of the clicked item as follow:

private void toolStripMenuItem_Click(object sender, EventArgs e)
{
(((sender as ToolStripItem).Owner as ContextMenuStrip).SourceControl as ListView)
    .Items.Cast<ListViewItem>().ToList().ForEach(lvi => lvi.Checked = true); 
}

Or use lvi.Checked = !lvi.Checked if you want to toggle the Checked property.