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?
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?
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;
}
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.