86723

VB.Net Get The Control That Is Used To Show The Contextmenu Strip

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

Recommend

  • Populate Drop Down List using jquery from textbox
  • Change one part of the selection with Regex
  • How to extract text in nested xml after closing of one tag in python using xml.etree.ElementTree
  • VB 2010 INSERT INTO syntax error
  • trying to create a 2 step code “if statement” [closed]
  • Add a textbox to a wpf canvas in a position chosen by the user (C#)
  • Removing spaces and special characters from string
  • PHP isn't working with SQLite
  • Radcombobox inside Radgrid FormTemplate
  • Reading strings from stdin
  • How do I find the string index of a tag (an element) without counting expanded entities?
  • setuid() failing - operation not permitted
  • how to get selected text range and add color to the particular selected text
  • c - Segmentation fault when trying to write a modified structure
  • WatiN can't find text after searching google
  • Jquery - Copy div contents to clipboard
  • How do you copy the contents of a Cell into the Clipboard
  • How to add brackets using AutoHotKey
  • replace text in a string with .net regexp
  • How to save a “CroppedBitmap”
  • How to work with Clipboard in ASP.NET, Server side
  • Replace all text in a rich text box
  • VB.NET How to send key 46 (Chr(46)) with SendMessage
  • .Net Compact Framework 3.5 HTTP POST
  • How To Understand dynamic HTML in Visual Studio 2008?
  • Problems using HOGDescriptor
  • Why is this code not working? Hangman
  • Insert audio album in Android MediaStore
  • Send data from edittext to listview
  • Understanding RTF and edit it with vb.net
  • How to getText() from the input field of an angularjs Application
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • trying to dynamically update Highchart column chart but series undefined
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal