Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. ToolStripMenuItem.Owner vs ContextMenuStrip

ToolStripMenuItem.Owner vs ContextMenuStrip

Scheduled Pinned Locked Moved .NET (Core and Framework)
visual-studiohelpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cybertone
    wrote on last edited by
    #1

    Yesterday i've received an interesting "feature"(or bug?). My form has context menu (ContextMenuStrip) with two levels: menuItem_1 -> menuItem_1_1 -> menuItem_1_2 menuItem_2 every menu item has Click event handler: void MenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; ContextMenuStrip menu = item.Owner as ContextMenuStrip; MessageBox.Show(menu == null ? "false" : "true"); } I've got: menuItem_1 click => true menuItem_1_1 click => false menuItem_1_2 click => false menuItem_2 click => true So, ToolStripMenuItem's Owner on 2+ level is not a ContextMenuStrip!!! :eek: Is it normal? I bet - not... And how I can receive the reference to the ContextMenuStrip to which the 2+ level menu item belongs? :confused:

    C 1 Reply Last reply
    0
    • C cybertone

      Yesterday i've received an interesting "feature"(or bug?). My form has context menu (ContextMenuStrip) with two levels: menuItem_1 -> menuItem_1_1 -> menuItem_1_2 menuItem_2 every menu item has Click event handler: void MenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem item = (ToolStripMenuItem)sender; ContextMenuStrip menu = item.Owner as ContextMenuStrip; MessageBox.Show(menu == null ? "false" : "true"); } I've got: menuItem_1 click => true menuItem_1_1 click => false menuItem_1_2 click => false menuItem_2 click => true So, ToolStripMenuItem's Owner on 2+ level is not a ContextMenuStrip!!! :eek: Is it normal? I bet - not... And how I can receive the reference to the ContextMenuStrip to which the 2+ level menu item belongs? :confused:

      C Offline
      C Offline
      chaiguy1337
      wrote on last edited by
      #2

      First you should check (in the debugger or by calling GetType()) what type the item.Owner actually is. I have a feeling for the submenus it would probably be ToolStripDropDown, not ContextMenuStrip. If you're looking to get a ref to the parent ContextMenuStrip, you might have to do something like ToolStripDropDown.OwnerItem.Owner. Logan

      {o,o}.oO( Did somebody say “mouse”? ) |)””’) -”-”-

      C 1 Reply Last reply
      0
      • C chaiguy1337

        First you should check (in the debugger or by calling GetType()) what type the item.Owner actually is. I have a feeling for the submenus it would probably be ToolStripDropDown, not ContextMenuStrip. If you're looking to get a ref to the parent ContextMenuStrip, you might have to do something like ToolStripDropDown.OwnerItem.Owner. Logan

        {o,o}.oO( Did somebody say “mouse”? ) |)””’) -”-”-

        C Offline
        C Offline
        cybertone
        wrote on last edited by
        #3

        Yes... item.Owner isn't ContextMenuStrip - it's type is ToolStripDropDownMenu. :suss: Construction like item.OwnerItem.Owner is not comprehensible, because: 1) It works for 2 level menu only, not for 3+ 2) For first level it throws exception Certainly, it is possible to use construction like: ToolStripItem parent = item; while(parent.OwnerItem != null)    parent = parent.OwnerItem; ContextMenuStrip menu = (ContextMenuStrip)parent.Owner; ... but it looks not as it would be desirable.
        P.S. What for the reference to object which property item.Owner (on 2+ level) refers is necessary to me? :confused:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups