2016-06-08 74 views
2

我将单个上下文菜单附加到多个文本框。所以,我需要获取用于显示上下文菜单的控件名称/引用。下面VB.Net获取用于显示上下文菜单条的控件

是我的上下文菜单中的样本图像:

enter image description here

下面是绿色的代码标记为“粘贴”项单击事件:

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 

它工作得很好。

,但下面是我的红色代码标记为“页数”项单击事件:

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) 

但上述抛出以下错误:

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

这里是错误的截图:

enter image description here

所以,我无法弄清楚什么是问题。

任何帮助,将不胜感激

+0

你不能只是钉在另一个'.Owner',因为你知道这是一个嵌套的菜单吗? – DonBoitnott

+0

请参阅此主题:http://stackoverflow.com/questions/12094528/contextmenustrip-owner-property-null-when-retrieving-from-nested-toolstripmenuit。我将在下面给出vb.net的答案,稍作改动。 –

回答

2

如果检查this C# thread接受的答案指出这是一个错误。此处介绍的解决方法使用私有变量将SourceControl存储在ContextMenuStripOpening事件中。我已转换为VB.NET并使用ContextMenuStripTag而不是使用该变量。然后,你指的是Tag属性,而不是故障SourceControl属性:

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 
+0

不是一个错误,只是一个误解。 SourceControl属性返回导致上下文菜单打开的控件。如果它为空,则不会导致上下文菜单打开。这将解决这个问题,但最好的办法是编写代码,以便不需要这些信息。我使用了大量的上下文菜单,从不需要这个。 –

+0

@CodyGray http://stackoverflow.com/questions/37703891/how-to-get-parent-control-from-context-menu-item-vb-net 这是我的问题,我希望你看一看在我的情况下,并建议最新的其他可靠的方法来解决这个问题?而不使用罗宾的方法。 –

+0

您的问题没有包含任何其他背景信息,可能会帮助我建议一种替代解决方案。没有提及*为什么*你正试图获得这些信息,或者你将要用它做什么。您只需显示您的建议解决方案(使用'SourceControl'属性),并显示它为什么不起作用。哪一种方法可以提出问题,但只能为您提供有限范围的答案。通常情况下,上下文菜单违反了SRP,需要了解应用程序中另一个控件的详细信息。 @ muh –