如何识别哪个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;
}
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(注释后)
sender
是ToolStripMenuItem
,所以使用强制转换来获得它,然后使用GetCurrentParent
方法来获得包含该项目的ContextMenuStrip
,然后使用SourceControl
属性来获得显示菜单的控件,如@sq33G:
ListView lv = ((ToolStripMenuItem) sender).GetCurrentParent().SourceControl;
也许您还需要将GetCurrentParent
返回值强制转换为ContextMenuStrip
。