如何识别哪个ListView被右键单击

本文关键字:ListView 右键 单击 何识别 识别 | 更新日期: 2023-09-27 18:14:27

我有一个ContextMenu分配给两个ListView。如何知道它与哪个ListView一起使用,以便我可以应用正确的方法?我猜sender在这里很重要,但我怎么使用它?如何知道sender在这个时刻是什么?

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

如何识别哪个ListView被右键单击

ContextMenu.SourceControl

是你的票。

http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenu.sourcecontrol.aspx

编辑

ContextMenuStrip,你说?

http://msdn.microsoft.com/en-us/library/system.windows.forms.contextmenustrip.sourcecontrol.aspx

您尝试了以下操作吗?

ListView listViewClicked = (ListView) sender;

EDIT(注释后)

senderToolStripMenuItem,所以使用强制转换来获得它,然后使用GetCurrentParent方法来获得包含该项目的ContextMenuStrip,然后使用SourceControl属性来获得显示菜单的控件,如@sq33G:

所建议的
ListView lv = ((ToolStripMenuItem) sender).GetCurrentParent().SourceControl;

也许您还需要将GetCurrentParent返回值强制转换为ContextMenuStrip