I have a C# WinForm application. On my Buttons I use the '&' (in the .Text properties) for
Hotkey access. The strange behavior is that
the '&' is converted to the appropriate underscore while
in design mode BUT DOES NOT appear at Runtime.

At runtime the Hotkey functionality works, but the
relevant underscores do not appear. For example, I have a
buttonSearch.Text = "&Search". At Runtime, the button
displays as "Search" and pressing "alt + S" triggers the event.

but the problem is that the underscore3 is not shown before i press alt !!!

Is this a bug? Or is there an obvious solution I have
overlooked. I do have KeyPreview = true.

Any suggestions would be greatly appreciated!
Thanks,

Dani AI

Generated

Short answer: this is not a WinForms bug in your code — it’s a combination of Windows’ keyboard-cue behavior and how ribbon controls render/access keys. As pointed out, Windows normally hides underline mnemonics until the user presses Alt; since sees them on standard buttons but not on ribbon controls, the ribbon you are using probably does not draw standard ampersand mnemonics and instead expects "key tips" or its own access-key mechanism. See the WinForms keyboard-cues property and the Ribbon framework docs for background. (Control.ShowKeyboardCues, )

Practical fixes you can apply right away:

  • Check the ribbon control’s documentation for a KeyTip / AccessKey / KeyTipText property and use that if available (Office-style ribbons expose key tips, not inline underlines).
  • If the ribbon has no built-in support, implement explicit shortcuts instead of relying on ampersands: either add a (visible or hidden) MenuStrip/ToolStripMenuItem and set ShortcutKeys, or handle shortcuts at the form level. Example override that catches a combo and triggers the search action:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Alt | Keys.S))
    {
        PerformSearch(); // call your search routine
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

For more control, ToolStripMenuItem.ShortcutKeys and ProcessCmdKey are documented here: (ShortcutKeys, ProcessCmdKey).

Do not try to force global OS settings to always show underlines — that changes a user preference. Use the ribbon’s key-tip API or explicit shortcut handling for consistent, discoverable keyboard access.

Recommended Answers

All 2 Replies

That's the default behavior of windows; it had nothing to to with your code/application. The underscores will only appear after you hit the alt key.

This expains how you can change it for windows 7 http://twigstechtips.blogspot.com/2009/08/windows-7-restore-menu-underline.html And i'm sure you can change it on other versions as well.

But if you run it on another pc, you'll have the same problem. So it's best not to worry about it, it really can't be helped

no its not that settings !! since on other normal buttons it works !! it doesn't work only on ribbon controls !! bdw thanks for yr reply !! what is another way of doing short cut keys?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.