ContextMenuItem and shortcut

  • Thread starter Thread starter dirk
  • Start date Start date
D

dirk

Hello

How do you get the control if the click event handler for ContextMenuItem
was invoked by the assigned shortcut? In this case the SourceControl
property may be incorrect if the context menu is shared by more than one
control. The SourceControl will be the last control that displayed the
context menu but the shortcut will not display the item.

Thanks
 
If the context menu is assigned to many controls and you invoke the Click of
one of it's menuitems via code or Shortcut Key, which one of the many
possibilities would you like to be assigned as the Source control?
 
Simply on the control that has input focus. So if I have 3 listviews and
press CTRL+A then the handler should operate on the one that has focus if
any. As I see it now I will only use a one to one relationship for control
and context menu. But then I won't need the SourceControl at any rate. It
seems that this property is especially useful for cases where the context
menu is shared though I cannot see how to handle the Shortcut key correctly.


"Mick Doherty"
 
I think the problem with that approach would be that the ActiveControl is
not necessarily the control that displayed the context menu. If I have two
data grids and select rows in one of them and then immediately right-click
on the other the ActiveControl is still the other data grid that did not
display the context menu.
To solve this problem one would need to know if the event handler was
invoked by the context menu or the shortcut but the sender is always the
MenuItem that is associated with the shortcut.



"Mick Doherty"
 
I think the approach I would use in this case is not to use a shortcut key.

You can display the ShortcutText in the MenuItem by editing the textvalue in
the MenuEditor as follows:
MenuItemText\tShortCut
the \t in the above line becomes a Tab therefore aligning Shortcut in a
second row.
Note that this must be done in the menu editor and not in the properties
editor

I would then handle this from the forms ProcessCmdKey() method.

As an added bonus, when using this approach you are not limited to the
ShortCutKey Enumeration.
 
Back
Top