Winforms MenuStrip Underlined Hotkey

Typically, hotkey letters in MenuStrip are underlined. (& File, & Open, etc.). In the project I'm working on, the underline appears in the designer, but not at runtime. I can not find a property that controls this. Somebody knows?

+6
winforms hotkeys underline menustrip
source share
4 answers

Windows has the option to show or not underline. To change the setting,

  • Right click on the desktop
  • Select Properties
  • Click the Appearance tab.
  • Click "Effects"
  • Uncheck "Hide underlined letters for keyboard navigation"
+9
source share

You can make the user see the underline by creating your own ToolStrip visualization tool. It took me a while to figure out how to get around Chris. Here is the creator I created:

using System; using System.Drawing; using System.Windows.Forms; namespace YourNameSpace { class CustomMenuStripRenderer : ToolStripProfessionalRenderer { public CustomMenuStripRenderer() : base() { } public CustomMenuStripRenderer(ProfessionalColorTable table) : base(table) { } protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e) { e.TextFormat &= ~TextFormatFlags.HidePrefix; base.OnRenderItemText(e); } } } 

And then in the form using MenuStrip in the constructor, you set the renderer:

 public YourFormConstructor() { InitializeComponents(); menuStripName.Renderer = new CustomMenuStripRenderer(); } 

I would like to note that if you prefer a visualization style to a system style, you can extend the ToolStripSystemRenderer class instead of Professional, but I like to customize the color table. This is a hotfix that does not require the client to change their computer settings. Enjoy it!

+13
source share

They will only be displayed at runtime when the user presses the alt key. When you press the alt key, the form thinks that you might want to use one of the shortcuts so that it displays any underline.

+7
source share

For the next question related to this, namely, that this works for all your application hotkeys:

 [DllImport ("user32.dll")] static extern void SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni); const uint SPI_SETKEYBOARDCUES = 0x100B; private static void GetAltKeyFixed() { //Set pvParam to TRUE to always underline menu access keys, int pv = 1; /* Call to systemparametersinfo to set true of pv variable.*/ SystemParametersInfo(SPI_SETKEYBOARDCUES, 0, ref pv, 0); } 

Call GetAltKeyFixed before Application.Run or equiv.

I need this to match 508, and almost all of the answers on this topic relate to changing desktop settings or the shorthand “why do you need to do this?” answer. This is not the craziest demand I've heard.

0
source share

All Articles