9

I add my menu items shortcut keys, but they are poorly arranged.

enter image description here

and I need to make it like this:

enter image description here

2
  • Just to clarify: The shortcut key combinations are right-aligned already. What this question is asking for is a menu with multiple left-aligned columns. The former is the default (today, anyway), likely because it produces a UI that's easier to comprehend. Commented Mar 18 at 11:30
  • Yep but 13 years ago aI had to make another align
    – Mediator
    Commented 2 days ago

3 Answers 3

9

Ah, I think I finally figured out how to do this (it had been puzzling me for quite some time how you'd gotten the two different screenshots...). It turns out there are two ways that you can instruct Windows to align shortcut keys in a drop-down menu.

The first (and probably most standard) way is to insert a tab character (\t) in the string corresponding to the menu text. This produces the bottom example shown in your original question, the one where all the identifiers are left-aligned and some overhang slightly. This is the standard in almost all Microsoft applications, and the only option that I knew existed until a few minutes ago.

     The first option, created using a tab character delimiter, with shortcut key sequences left-aligned     Sample resource string: &Print…\tCtrl+P

The second way is to replace that \t escape sequence with \a in the resource string (which, yes, strangely enough would normally indicate an alert bell). This causes Windows to right align all of the shortcut key sequences in the menu, producing the example illustrated in your first screenshot. This does produce a menu that uses screen space more efficiently (it's smaller), but this comes at the cost of a neat alignment of each of the shortcut key sequences down their left-hand margins, which I think makes for slightly easier readability.

     The second option, created using an '\a' delimiter, with shortcut key sequences right-aligned     Sample resource string: &Print…\aCtrl+P

So if you want your menus to look like the second example in your original question (Yes, I've confusingly arranged my samples backwards. Sorry), you need to delimit the shortcut key sequence with a tab character (\t) in the resource file containing your menu item text strings.

The strange thing is that you claim to be using .NET WinForms, which handles all of this automatically (saving you from the pain of messing with resource files). I know for a fact that it inserts tab characters, and all of the menus I've ever seen it generate do look like your second example.

The best thing to do is switch your menu to the old MainMenu control included with earlier version of the .NET Framework. (To do this, you'll probably have to right-click on the Toolbox and add the control manually—it isn't there by default on later versions of Visual Studio.) This will ensure that you see the alignment behavior you expect, consistent with all of the standard Windows applications like Notepad. It also produces menus that look like the native operating system menus (in Vista and 7, they are painted blue) rather than the amateur-looking owner-drawn menus produced by the MenuStrip control that are completely out-of-place in modern versions of Windows.

Microsoft's official documentation does confirm that the MainMenu control is still supported for future use, so there's no reason to fear using it in your apps. I highly recommend everyone use this instead:

Although MenuStrip replaces and adds functionality to the MainMenu control of previous versions, MainMenu is retained for both backward compatibility and future use if you choose.

1
  • 1
    \a (alert bell) does not work when using AppendMenu(..., "&Open\aCtrl+O"); , do you know how to use this thing using 'AppendMenu' (without using resource file) ?
    – dns
    Commented Jan 20, 2014 at 10:41
5

They are 'right' aligned, as you can see. (No pun intended).

Right

This is default behaviour for a menu. You'll need custom drawing to do it differently.

2

Every OS has its particular look and feel, and I guess that you have to have pretty good reasons not to honor how every other application on the desktop looks. I guess you will either drop the issue, or will extend the menu with OwnerDrawn items.

Here is the overkill article on the subject.

1
  • "Every OS has it's particular look and feel" And yet most Windows software (or at the very least Visual Studio itself) actually does what the OP is asking to do. I.e. the OP is asking how to make their software look more like something one would expect from Microsoft because the default is (quite vexingly) actually different to the native OS behaviour.
    – Pharap
    Commented Apr 17, 2022 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.