MenuStrip Erroneously Closing
-
I have a MenuStrip that contains ToolStripMenuItem's with submenus. In a few of the submenus, I have two ToolStripMenuItem's to move up or down a list of options with keyboard shortcuts. For example:
RootMenuItem (e.g. File)
SubItem0 (e.g. New)
SubItem1
SelectPrevious (Ctrl+Left)
SelectNext (Ctrl+Right)
---------------------------
Option 0
Option 1
...
Option N
SubItem2
SelectPrevious (Ctrl+Up)
SelectNext (Ctrl+Down)
--------------------------
Option 0
Option 1
...
Option MNow, all of the "Option N" ToolStripMenuItem's have their CheckOnClick property set to true, and they all have the same event handler. When I click the "SelectPrevious" or "SelectNext" options, everything works fine (Option N is unchecked, and Option (N - 1) or (N + 1) is checked, as appropriate). In addition, if the final option is selected SelectNext is disabled, and the same for SelectPrevious when the first option is selected. Here is the problem: If I use any of the keyboard shortcuts for SelectPrevious or SelectNext, the next time I hover over the associated SubItem, then hover over a different SubItem, the entire menu is closed. Here are the steps to reproduce:
1. RootMenuItem->SubItem1->SelectPrevious using the shortcut Ctrl+Left
2. Hover over RootMenuItem->SubItem1 until the submenu appears
3. Move the mouse over RootMenuItem->SubItem0 (or SubItem2)After step 3, the entire menu closes. The next time I attempt to open the menu (without using a keyboard shortcut in between), everything works fine. Basically, every time I use the keyboard shortcuts any number of consecutive times, the next time I open the submenu that hosts a ToolStripMenuItem that was clicked via a keyboard shortcut, the menu closes and resets to some "good" state that allows it to work correctly the next time. Has anyone else experienced this? Is there any way to fix it, or is it just a .Net bug? I'm using .Net 4.0 on Windows XP SP3, in case it matters. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
I have a MenuStrip that contains ToolStripMenuItem's with submenus. In a few of the submenus, I have two ToolStripMenuItem's to move up or down a list of options with keyboard shortcuts. For example:
RootMenuItem (e.g. File)
SubItem0 (e.g. New)
SubItem1
SelectPrevious (Ctrl+Left)
SelectNext (Ctrl+Right)
---------------------------
Option 0
Option 1
...
Option N
SubItem2
SelectPrevious (Ctrl+Up)
SelectNext (Ctrl+Down)
--------------------------
Option 0
Option 1
...
Option MNow, all of the "Option N" ToolStripMenuItem's have their CheckOnClick property set to true, and they all have the same event handler. When I click the "SelectPrevious" or "SelectNext" options, everything works fine (Option N is unchecked, and Option (N - 1) or (N + 1) is checked, as appropriate). In addition, if the final option is selected SelectNext is disabled, and the same for SelectPrevious when the first option is selected. Here is the problem: If I use any of the keyboard shortcuts for SelectPrevious or SelectNext, the next time I hover over the associated SubItem, then hover over a different SubItem, the entire menu is closed. Here are the steps to reproduce:
1. RootMenuItem->SubItem1->SelectPrevious using the shortcut Ctrl+Left
2. Hover over RootMenuItem->SubItem1 until the submenu appears
3. Move the mouse over RootMenuItem->SubItem0 (or SubItem2)After step 3, the entire menu closes. The next time I attempt to open the menu (without using a keyboard shortcut in between), everything works fine. Basically, every time I use the keyboard shortcuts any number of consecutive times, the next time I open the submenu that hosts a ToolStripMenuItem that was clicked via a keyboard shortcut, the menu closes and resets to some "good" state that allows it to work correctly the next time. Has anyone else experienced this? Is there any way to fix it, or is it just a .Net bug? I'm using .Net 4.0 on Windows XP SP3, in case it matters. Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
I can't say I've seen this exact issue but the mention of shortcut keys and disabled menu items made me recall something similar. I had a contextmenustrip where items were enabled or disabled dynamically in the Opening event. The shortcut keys were a bit erratic and eventually I discovered my mistake. What I hadn't understood was that a shortcut calls the item's click event handler directly but only if the item is enabled. The shortcut did NOT trigger the menu Opening event which I was relying on to set the item enabled states correctly. Is your problem caused by some logical fault related to assumed enable states of menu items? Alan.
-
I can't say I've seen this exact issue but the mention of shortcut keys and disabled menu items made me recall something similar. I had a contextmenustrip where items were enabled or disabled dynamically in the Opening event. The shortcut keys were a bit erratic and eventually I discovered my mistake. What I hadn't understood was that a shortcut calls the item's click event handler directly but only if the item is enabled. The shortcut did NOT trigger the menu Opening event which I was relying on to set the item enabled states correctly. Is your problem caused by some logical fault related to assumed enable states of menu items? Alan.
Thanks for posting a response! However, I don't think that is the issue in this case. The only events I am subscribed to are the click events on leaf items (definition: leaf items are menu items without submenus). All of the buttons in all of my menus are coming out with the appropriate checked and enabled states. The problem is just that something is happening to tweak the menu state when I use the shortcut keys instead of clicking the button through the GUI. None of my code is behaving differently depending on whether the menu items are clicked via shortcut keys, using the keyboard, or with the mouse. This has to be a .Net anomaly, but I was hoping someone else had seen this issue and knew a work-around. My next step is to comment out each line of code in my click event handler, to see if removing any of the commands makes this work so I can be more specific on what the problem is. Thanks for your time,
Sounds like somebody's got a case of the Mondays -Jeff