Winforms MenuStrip Underlined Hotkey Letter
Asked Answered
A

5

7

Typically the hotkey letters on a MenuStrip are underlined. (&File, &Open, etc) On a project I'm working on the underline shows up in the designer, but not at run time. I can't find the property that controls this. Anyone out there know?

Abiosis answered 6/5, 2009 at 15:58 Comment(1)
Do they show up when you press the Alt key?Eros
I
10

In Windows, there is a setting whether or not to show the underline. To change the setting,

  1. Right click on the desktop
  2. Select "Properties"
  3. Click on the "Appearance" tab
  4. Click the "Effects" button
  5. Uncheck the box labeled "Hide underlined letters for keyboard navigation"
Insole answered 6/5, 2009 at 16:5 Comment(4)
Nice one. I knew that setting was somewhere, but remember how to get to it.Cham
Thanks, that was it. Feel kind of dumb asking the question now. :-)Abiosis
I mean does that really solve the problem? Any user who gets your application will need to either manually change that setting or you will need to add something to a setup application in order to change the setting automatically.Odellodella
Please check my post below for a better solutionSpanjian
S
15

You can force the user to see the underline by creating a custom ToolStrip renderer. It took me quite a while to figure out how to bypass Chris's answer. Here is the renderer 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 with the 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 System style look of renderering you can extend the ToolStripSystemRenderer class instead of the Professional but I like being able to customize the color table. This is the fix that doesn't require the client to change his computer settings. Enjoy!

Spanjian answered 8/8, 2009 at 14:16 Comment(3)
Thanks Cris, you provided exactly what I was looking for.Scutt
Would work something like that but applied to command buttons (wich also can have a hot key)??Rarefied
Great solution. There is a small typo on ToolStripProfressionalRenderer. It should be ToolStripProfessionalRenderer.Caseycash
I
10

In Windows, there is a setting whether or not to show the underline. To change the setting,

  1. Right click on the desktop
  2. Select "Properties"
  3. Click on the "Appearance" tab
  4. Click the "Effects" button
  5. Uncheck the box labeled "Hide underlined letters for keyboard navigation"
Insole answered 6/5, 2009 at 16:5 Comment(4)
Nice one. I knew that setting was somewhere, but remember how to get to it.Cham
Thanks, that was it. Feel kind of dumb asking the question now. :-)Abiosis
I mean does that really solve the problem? Any user who gets your application will need to either manually change that setting or you will need to add something to a setup application in order to change the setting automatically.Odellodella
Please check my post below for a better solutionSpanjian
O
8

They will only show up at runtime when the user presses the alt key. When you press the alt key the form thinks you might want to use one of the shortcuts so it displays any of the underlining.

Odellodella answered 6/5, 2009 at 16:2 Comment(0)
E
1

For the follow-up question related to this, namely, getting this to work for all your applications 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 prior to the Application.Run or equiv.

I needed this for 508 compliance and almost all answers on this subject are to change the Desktop settings Or a terse "why would you want to do that?" response. This is not the craziest requirement I've heard off.

Extremely answered 8/10, 2015 at 22:55 Comment(0)
A
0

In windows 10 the setting is in

Settings - Ease of Access - Keyboard - Underline access keys when available

in the Change how keyboard shortcuts work section.

Aback answered 29/9, 2021 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.