Getting ToolStripItem that initiated ContextMenuStrip by keyboard
In WinForms, only ToolStrip can have ContextMenuStrip, not ToolStripItem.
As I need ContextMenuStrip to know which button user refers to, I have a solution when button is pressed by mouse (by getting cursor position at ContextMenuStrip.Openning, and then getting item from ToolStrip), but I have no solution for keyboard selection. In theory, one could try to capture OnKeyDown of ToolStrip and try to track which item is selected by user action, but in practice if ToolSt开发者_Python百科rip is overflowed it might get pretty complicated and probably unreliable.
Is there any solution for this?
SOLUTION:
I played a little, and here is the solution, in ContextMenuStrip.Opening handler do this:
toolStrip.Items.Cast<ToolStripItem>().Where(x => x.Selected).First()
to get item that was selected when key was pressed to show ContextMenuStrip.
I played a little, and here is the solution, in ContextMenuStrip.Opening handler do this: toolStrip.Items.Cast<ToolStripItem>().Where(x => x.Selected).First()
to get item that was selected when key was pressed to show ContextMenuStrip.
精彩评论