把我的ContextMenuStrip的SourceControl当我使用的快捷键 [英] Get the SourceControl of my ContextMenuStrip when I use the shortcut key
问题描述
我有一个的ContextMenuStrip
连接到两个控件( DataGridView的
)。结果
在 ToolStripMenuItem
单击事件,我设法得到原始调用者(即 DataGridView的
)使用此代码:
I have single ContextMenuStrip
attached to two controls (DataGridView
).
In the ToolStripMenuItem
click event, I manage to get the original caller (the DataGridView
) with this code :
var menu = (ToolStripDropDownItem)sender;
var strip = (ContextMenuStrip)menu.Owner;
var dgv = (DataGridView)strip.SourceControl;
当我点击我的 ToolStripMenuItem $ C $它的工作原理相当不错C>。结果
但是当我使用连接到 ToolStripMenuItem
,带钢。 SourceControl $ C $的sortcut关键C>返回NULL。结果
有谁知道为什么吗?
It works pretty good when I click on my ToolStripMenuItem
.
But when I use the sortcut key linked to the ToolStripMenuItem
, the strip.SourceControl
return null.
Does anyone know why ?
推荐答案
该SourceControl属性显示导致控制在的ContextMenuStrip打开。因为在这种情况下的ContextMenuStrip不显示没有用于打开它的控制,因此,属性是空
The SourceControl property shows the control that caused the ContextMenuStrip to open. Since in this case the ContextMenuStrip doesn't display there is no control used to open it, and thus the property is null.
这个属性是在的上下文中更好地用于开幕活动。请参见 ContextMenuStrip.SourceControl 。
This property is better used in the context of the opening event. See ContextMenuStrip.SourceControl.
更新:一种方法找出哪些DataGridView的是ToolStripMenuItem点击的接收者会,看看哪一个具有焦点:
Update: One method to figure out which DataGridView was the intended receiver of the ToolStripMenuItem click would be to see which one has the focus:
var dgv = this.ActiveControl as DataGridView;
if ( dgv != null) // make sure to check for null before trying to use this var
//...
这篇关于把我的ContextMenuStrip的SourceControl当我使用的快捷键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!