把我的ContextMenuStrip的SourceControl当我使用的快捷键

编程入门 行业动态 更新时间:2024-10-28 18:35:01
本文介绍了把我的ContextMenuStrip的SourceControl当我使用的快捷键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个的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 。结果但是当我使用连接到 ToolStripMenuItem ,带钢。 SourceControl 返回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当我使用的快捷键

本文发布于:2023-11-03 09:18:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554793.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   快捷键   ContextMenuStrip   SourceControl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!