VB.Net获取用于显示上下文菜单条的控件 [英] VB.Net Get The Control That Is Used To Show The Contextmenu Strip

查看:116
本文介绍了VB.Net获取用于显示上下文菜单条的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将单个上下文菜单附加到多个文本框。因此,我需要获取用于显示上下文菜单的控件名称/引用。

i am attaching a single context menu to multiple text box. so, i need to get the control name/reference that used to show the context menu.

下面是我的上下文菜单的示例图像:

below is the sample image of my context menu:

以下是带有绿色标记的粘贴项目点击事件的代码:

Below is the code for green marked "paste" item click event:

    Dim objTSMI As ToolStripMenuItem
    Dim objCMS As ContextMenuStrip
    Dim objTxtBox As System.Windows.Forms.TextBox
    objTSMI = CType(sender, ToolStripMenuItem)
    objCMS = CType(objTSMI.Owner, ContextMenuStrip)
    objTxtBox = CType(objCMS.SourceControl, System.Windows.Forms.TextBox)
    If Clipboard.ContainsText(TextDataFormat.Text) = True Then
        objTxtBox.SelectedText = Clipboard.GetText(TextDataFormat.Text)
    End If

它很好用。

但下面是我的鳕鱼e表示红色的页面计数项单击事件:

but below is my code for red marked "Page count" item click event:

    Dim objTSMI As ToolStripMenuItem
    Dim objCMS As ContextMenuStrip
    Dim objTxtBox As System.Windows.Forms.TextBox
    objTSMI = CType(sender, ToolStripMenuItem)
    objCMS = CType(objTSMI.Owner, ContextMenuStrip)
    objTxtBox = CType(objCMS.SourceControl, System.Windows.Forms.TextBox)
    MessageBox.Show(objTxtBox.Name)

但是以上抛出以下错误:

but above throws following error :

Unable to cast object of type 'System.Windows.Forms.ToolStripDropDownMenu' to type 'System.Windows.Forms.ContextMenuStrip'.

以下是错误的屏幕截图:

here is the screenshot of the error:

所以,我不知道是什么问题。

so, i can't figure it out what is the issue.

任何帮助将不胜感激

推荐答案

如果选中<一个href = https://stackoverflow.com/questions/12094528/contextmenustrip-owner-property-null-when-retrieving-from-nested-toolstripmenuit>此C#线程接受的答案指出这是一个错误。此处介绍的解决方法使用私有变量将 SourceControl 存储在 Opening 事件上ContextMenuStrip 。我已转换为VB.NET,并使用了 ContextMenuStrip Tag 而不是使用变量。然后,您引用 Tag 属性而不是错误的 SourceControl 属性:

If you check this C# thread the accepted answer notes it is a bug. The workaround presented there uses a private variable to store the SourceControl on the Opening event of the ContextMenuStrip. I've converted to VB.NET and used the Tag of the ContextMenuStrip instead of using the variable. You then refer to the Tag property instead of the faulty SourceControl property:

Imports System.ComponentModel

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.TextBox1.ContextMenuStrip = Me.ContextMenuStrip1
        Me.TextBox2.ContextMenuStrip = Me.ContextMenuStrip1
    End Sub

    Private Sub ContextMenuStrip1_Opening(sender As Object, e As CancelEventArgs) Handles ContextMenuStrip1.Opening
        Me.ContextMenuStrip1.Tag = CType(Me.ContextMenuStrip1.SourceControl, Control)
    End Sub

    Private Sub TestToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TestToolStripMenuItem.Click
        ' first level of context menu strip
        Dim Strip As ContextMenuStrip = CType(sender, ToolStripMenuItem).Owner
        Dim Box As TextBox = Strip.Tag

        MessageBox.Show(Box.Name)
    End Sub

    Private Sub ChildToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ChildToolStripMenuItem.Click
        ' second level of context menu strip
        Dim Strip As ContextMenuStrip = CType(sender, ToolStripMenuItem).OwnerItem.Owner
        Dim Box As TextBox = Strip.Tag

        MessageBox.Show(Box.Name)
    End Sub

End Class

这篇关于VB.Net获取用于显示上下文菜单条的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆