4

I've created a form with ContextMenuStrip. I set its shortcut using Text field in following way: "&File". However, when I open this context menu by right mouse button click, underscore is shown only when I simultaneously hold Alt button. Is there a way to show underscore on a mouse click without holding Alt button?

Sergey Dudkin
  • 169
  • 1
  • 10
  • take a look at this: http://stackoverflow.com/questions/10163617/is-there-a-way-to-force-always-show-mnemonics-in-menus – Tzu Apr 22 '15 at 14:13
  • I am not sure, that is why I am asking :) Maybe there is some sort a workaround, to show it not depending on setting? – Sergey Dudkin Apr 22 '15 at 14:14
  • 1
    Yes, it is a user preference, you should not ignore it. Control Panel > Ease of Access Center > Make the keyboard easier to use > Underline keyboard shortcuts and access keys. – Hans Passant Apr 22 '15 at 14:39

1 Answers1

3

You can modify the text rendering behaviour (HidePrefix) via a custom ToolStripSystemRenderer:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            contextMenuStrip1.Renderer = new AccessKeyMenuStripRenderer();
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(Cursor.Position);
        }
    }

    class AccessKeyMenuStripRenderer : ToolStripSystemRenderer 
    {
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextFormat &= ~TextFormatFlags.HidePrefix;
            base.OnRenderItemText(e);
        }
    }
}
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Wow, thank you, that is exactly what I need! Could you please tell me, where can I read more about Renderer? – Sergey Dudkin Apr 22 '15 at 14:24
  • See the "How To" links at the bottom of: https://msdn.microsoft.com/en-us/library/system.windows.forms.toolstrip.renderer%28v=vs.110%29.aspx – Alex K. Apr 22 '15 at 14:27