我已将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;
}
答案 0 :(得分:1)
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
答案 1 :(得分:0)
您是否尝试过以下操作?
ListView listViewClicked = (ListView) sender;
编辑(评论后)
sender
是ToolStripMenuItem
,因此请使用强制转换来获取它,然后使用GetCurrentParent
方法获取包含该项目的ContextMenuStrip
,然后使用{{ 1}}属性以获取显示菜单的控件,如 @ sq33G 所示:
SourceControl
也许您还需要将ListView lv = ((ToolStripMenuItem) sender).GetCurrentParent().SourceControl;
返回值转换为GetCurrentParent
。