I am creating a contextmenu that should contain a listing of all folders, subfolders, and files in a chosen folder. I want the menu to open the files and folders when they are clicked, but the click event doesn't register if the menuitem has subitems.
void Foo(string Title)
{
MenuItem = new MenuItem(Title);
MenuItem.Click += new EventHandler(MenuItem_Click);
ContextMenu.MenuItems.Add(MenuItem);
}
void MenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("This box will only show when menuitems without subitems are clicked");
}
How can I make the click event fire even if the menuitem has subitems?
It sounds like a menu may not be the most appropriate UI widget here. I believe the reason you don't get a click event raised is that menu items with submenus are assumed to only expand their children when clicked, rather than executing any other action.
That's likely to be the assumption of the user, too.
This behaviour is mentioned in the documentation for MenuItem.Click
:
Note: If the MenuItems property for the MenuItem contains any items, this event is not raised. This event is not raised for parent menu items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With