Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Remove Automatic Accelerator Keys For Delphi Menu Item Remove Auto & in Items Caption

Title: Remove Automatic Accelerator Keys For Delphi Menu Item - Remove Auto "&" in Item's Caption By default the AutoHotkeys property of a Delphi TMainMenu or TPopupMenu has the value of maAutomatic. The AutoHotkeys property determines if the menu should automatically ensure that accelerator keys for its items are reset if necessary so that every menu item has an accelerator key and no two items have the same accelerator key. With AutoHotkeys set to maAutomatic, if you have not provided an accelerator key for a menu item(in its Caption) the "&" character will be added to the Caption. In most cases you would benefit from such an automatic feature, EXCEPT when you need to compare the menu item Caption value to some string (expected value of the Caption). Real World Problem With AutoHotkeys = maAutomatic I am using a popup menu for some of the buttons in a toolbar in my application. The toolbar button, when clicked, displays a popup menu with options to configure the toolbar button action. The items from the menu are further displayed in a tree like component to allow the user to quickly setup options. Needing to check what popup menu item corresponds to what tree node I'm comparing the Caption of the menu item to the Text displayed by the tree node. And here's the problem: Having the Caption property of the menu item set to (at design time) "Set this" the code below returns false! if aMenuItem.Caption := 'Set this' then ... The problem is in the AutoHotkeys property that, by default, is set to "maAutomatic". At run time, the Caption would equal '&Set this'! Set AutoHotkeys = maManual for Run Time Caption Comparison To fix the above problem a simple solution is to set AutoHotkeys to maManual for the TPopupMenu OR to set AutoHotkeys := maManual for a single menu item (where default is maParent). Also, you could be using the RTL's StripHotKey() function. StripHotkey removes the & escape char that marks the accelerator characters in Text.