ToolStripMenuItem.Owner vs ContextMenuStrip
-
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: -
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: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”? ) |)””’) -”-”-
-
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”? ) |)””’) -”-”-
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: