0

I'm trying to get the source control name of my context menu. It works fine when you are casting ToolStripMenuItem and ContextMenuStrip, but I have one drop down ToolStripMenuItem too and It doesn't work same. I tried different approaches using casting ToolStripDropDownItem, ToolStripDropdown, ToolStripMenuItem but I'm allways getting "Object reference not set to an instance of an object." error. A few tries here (both same errors):

Private Sub MicrosoftWordToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MicrosoftWordToolStripMenuItem.Click

  'Dim BtnbWord As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
  'Dim BtnNew As ToolStripMenuItem = CType(BtnWord.OwnerItem, ToolStripMenuItem)
  'Dim Kontexst_M As ContextMenuStrip = CType(BtnNew.Owner, ContextMenuStrip)

   Dim BtnWord As ToolStripDropDownItem = CType(sender, ToolStripDropDownItem)
   Dim BtnNew As ToolStripDropDown = CType(BtnWord.Owner, ToolStripDropDown)
   Dim Kontext_M As ContextMenuStrip = CType(BtnNew.OwnerItem.Owner, ContextMenuStrip)

   'I need source control name to handle my click event of this toolstrip item    
   If Kontext_M.SourceControl.Name= "Listview1" then
           Msgbox("Listview1")
   End If

Any ideas on how I could get source control name ?...In case you are wondering, I need this to handle right click for creating Word documents:

enter image description here

LuckyLuke82
  • 586
  • 1
  • 18
  • 58
  • I am not sure what you are trying to do. A `ContextMentuStrip` has a `SourceControl` property, but the Word-Excel-Folder set are `ToolStripMenuItems` which do not. – Ňɏssa Pøngjǣrdenlarp Dec 03 '16 at 14:48
  • @Plutonix, I have 2 Listviews, and I add Items in It (including File.System changes). First I need to determine what Listview I'm in with Context Menu, to peform actions on click. I use 1 context menu for both Listviews. I did manage It to work like this, but only with ToolStripMenuItem. This "New" ToolStripMenuItem is recognized as "TollStripDropDown" if I do same thing here. – LuckyLuke82 Dec 03 '16 at 15:01
  • 1
    http://stackoverflow.com/q/31429616/1070452 – Ňɏssa Pøngjǣrdenlarp Dec 03 '16 at 15:03
  • @Plutonix, nice workaround. However, I had to do a little bit different, as you suggested in that link brings me an empty result. Please take a look at my posted answer If It's fine, I'm worried about others that said It's buggy. also, post your answer, I will delete mine and take you credit for It. – LuckyLuke82 Dec 03 '16 at 15:34
  • If that helped then upvote it - you can upvote *any* Q or A that helps not just the ones posted on your Qs. Whether you capture the source control in opening or click isnt much difference, you could also just close this as a dupe of that. – Ňɏssa Pøngjǣrdenlarp Dec 03 '16 at 15:44

1 Answers1

1

This is what I used to get the source control of the 'sub context menu':

 Dim sc As Control = CType(CType(sender.Owner, ToolStripDropDownMenu).OwnerItem.Owner, ContextMenuStrip).SourceControl
PKanold
  • 139
  • 7