ContextMenuStrip的SourceControl在ToolStripMenuItem中点击什么都没有?

发布于 2024-09-06 17:04:27 字数 711 浏览 6 评论 0原文

我有一个 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.SourceControlNothing

您知道在这种情况下出了什么问题或者为什么未设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏日落 2024-09-13 17:04:27

我找到了一个解决方法,但它并不是问题的真正答案。所以我会把它打开一段时间。

我使用 ContextMenuStrip Opening 事件在本地存储源对象。

Private Sub contextGrid_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles contextGrid.Opening

  _ContextSourceGrid = DirectCast(contextGrid.SourceControl, DataGridView)

End Sub

并直接引用所有 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.

Private Sub contextGrid_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles contextGrid.Opening

  _ContextSourceGrid = DirectCast(contextGrid.SourceControl, DataGridView)

End Sub

and refer directly to the saved object inside all ToolStripMenuItem Click events.

浅暮の光 2024-09-13 17:04:27

试试这个:

DirectCast(DirectCast(DirectCast(
    tsmi.Owner, System.Windows.Forms.ToolStrip).
    TopLevelControl, System.Windows.Forms.Control),
    System.Windows.Forms.ContextMenuStrip).SourceControl

Try this:

DirectCast(DirectCast(DirectCast(
    tsmi.Owner, System.Windows.Forms.ToolStrip).
    TopLevelControl, System.Windows.Forms.Control),
    System.Windows.Forms.ContextMenuStrip).SourceControl
时光磨忆 2024-09-13 17:04:27

几周前我也有同样的问题,实际上,没有人能弄清楚为什么会发生这种行为。看看我问的问题,我得到的解决方案效果很好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文