MadBoy
MadBoy

Reputation: 11104

How to identify which ListView was rightclicked ?

I have a ContextMenu assigned to two ListViews. How to know which ListView it was used with so I can apply correct method? I guess sender is important here but how do I use it? How to know what sender is at this very moment?

private void contextMenuDokumentyDodaj_Click(object sender, EventArgs e) {
    var dokumenty = new DocumentsGui(varKlienciID, varPortfelID);
    dokumenty.Show();
    dokumenty.FormClosed += varDocumentsGui_FormClosed;
}

Upvotes: 1

Views: 519

Answers (2)

Otiel
Otiel

Reputation: 18743

Did you try the following?

ListView listViewClicked = (ListView) sender;

EDIT (after comments)

The sender is the ToolStripMenuItem, so use a cast to get it, then use GetCurrentParent method to get the ContextMenuStrip containing the item, then use the SourceControl property to get the control displaying your menu, as suggested by @sq33G:

ListView lv = ((ToolStripMenuItem) sender).GetCurrentParent().SourceControl;

Maybe you'll need also to cast the GetCurrentParent return value to a ContextMenuStrip.

Upvotes: 0

Related Questions