Search code examples
c#winformstoolstripdropdowntoolstripmenu

How do I close a toolstripmenuitem that is set to autoclose = false?


I have a menu of items that the user can toggle. I wanted the menu to stay open so the user can check all the items they want. I set autoclose = false and now that works great. However, I also cannot close the window now lol. I tried clicking off of the menu onto the form, hitting escape, hitting the menu item, hitting the keycombo for the menu, nothing works.

Ideally, I'd like the user to be able to just click the form or basically anything but the menu to close it or press escape. How would I accomplish that? I tried creating a gotfocus event on the form and doing item.HideDropDown in there but no dice.

Thanks!


Solution

  • Generate the click event for the form, and then go through and for every control that doesn't have its own click event, set its click event to the one for the form.

    In the event, include the code to hide the menu: toolStripDropDownButton.HideDropDown();

    Copy the code to any existing click events for other controls.

    This is how I handled hiding a monthcalendar when you click anywhere on the form.

    And if you want to also include pressing escape as an option, do the same thing with a KeyDown event, checking if it's the escape key before running the code.