
Question:
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:
<a href="https://i.stack.imgur.com/QCyzC.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/QCyzC.png" data-original="https://i.stack.imgur.com/QCyzC.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
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
it works very fine.
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:
<a href="https://i.stack.imgur.com/AVMhR.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/AVMhR.png" data-original="https://i.stack.imgur.com/AVMhR.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
so, i can't figure it out what is the issue.
any help would be highly appreciated
Answer1:If you check <a href="https://stackoverflow.com/questions/12094528/contextmenustrip-owner-property-null-when-retrieving-from-nested-toolstripmenuit" rel="nofollow">this C# thread</a> 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