ContextMenuStrip的SourceControl在ToolStripMenuItem中点击什么都没有?
我有一个 ContextMenuStrip 附加到更多控件。
使用 ContextMenuStrip 的 Opening 事件来过滤/禁用某些上下文条目。 在这种情况下,属性 ContexteMenuStrip.SourceControl
设置正确。
我遇到的问题是 ToolStripMenuItem 的 Click 事件。该项目位于 ToolStripDropDown
内。
我用代码获取父项:
Dim tsmi As ToolStripMenuItem = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ToolStripDropDown).OwnerItem, ToolStripMenuItem)
然后我得到 ContextMenuStrip:
Dim contextMenu As ContextMenuStrip = DirectCast(tsmi.Owner, ContextMenuStrip)
但是现在,如果我检查 contextMenu.SourceControl
是 Nothing
。
您知道在这种情况下出了什么问题或者为什么未设置 SourceControl 吗?
先感谢您
I have single ContextMenuStrip
attached to more controls.
In use the Opening
event of ContextMenuStrip
to filter/disable some context entries.
In this case the property ContexteMenuStrip.SourceControl
is set correctly.
The problem I have is on the Click event of a ToolStripMenuItem
. This item is inside a ToolStripDropDown
.
I get the parent item with code:
Dim tsmi As ToolStripMenuItem = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ToolStripDropDown).OwnerItem, ToolStripMenuItem)
then I get the ContextMenuStrip:
Dim contextMenu As ContextMenuStrip = DirectCast(tsmi.Owner, ContextMenuStrip)
but now, if I check contextMenu.SourceControl
is Nothing
.
Do you have any idea what is wrong or why SourceControl is not set in this case?
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了一个解决方法,但它并不是问题的真正答案。所以我会把它打开一段时间。
我使用
ContextMenuStrip
Opening
事件在本地存储源对象。并直接引用所有
ToolStripMenuItem
Click
事件中保存的对象。I found a workaround that is not really the answer to the question. So I will leave it open for a while.
I used the
ContextMenuStrip
Opening
event to store locally the source object.and refer directly to the saved object inside all
ToolStripMenuItem
Click
events.试试这个:
Try this:
几周前我也有同样的问题,实际上,没有人能弄清楚为什么会发生这种行为。看看我问的问题,我得到的解决方案效果很好。
I had this exact same question a couple weeks ago, and in reality, nobody could figure out why this behavior was occurring. Take a look at the question I asked, the solution I was given worked great.