Library sample chapters

Introduction to Windows Forms

Menu Layout

Menus are built up from MenuItem components. These can be arranged across the screen on the menu bar, and are most often arranged vertically in drop-down menus. You can change the default layout of MenuItems to give a different UI style.

The Break and BarBreak methods are used to create menus that are arranged horizontally rather than vertically. Setting the BarBreak property in a MenuItem causes the item to be drawn in a new column. BarBreak adds a vertical separator bar to the menu between the columns. Break makes a new column but doesn't add the vertical bar. The modifications to the menus.cs code on lines 14 and 20 in the following result in the change seen in Figure 3.1.7.

   MenuItem filemenu = new MenuItem();
   filemenu.Text = "&File";
   menu.MenuItems.Add(filemenu);

     MenuItem open = new MenuItem();
     open.Text = "&Open";
     open.Select += new EventHandler(ShowInfo);
     filemenu.MenuItems.Add(open);

     MenuItem save= new MenuItem();
     save.Text = "&Save";
     save.Select += new EventHandler(ShowInfo);
     filemenu.MenuItems.Add(save);
     save.BarBreak=true;

     MenuItem exit= new MenuItem();
     exit.Text = "E&xit";
     exit.Select += new EventHandler(ShowInfo);
     filemenu.MenuItems.Add(exit);
     exit.Break=true;

Figure 3.1.7
The BarBreak property.

Similarly, the code changes to lines 14 and 20 in the following result in the menu style shown in Figure 3.1.8.

   MenuItem filemenu = new MenuItem();
   filemenu.Text = "&File";
   menu.MenuItems.Add(filemenu);

     MenuItem open = new MenuItem();
     open.Text = "&Open";
     open.Select += new EventHandler(ShowInfo);
     filemenu.MenuItems.Add(open);

     MenuItem save= new MenuItem();
    save.Text = "&Save";
    save.Select += new EventHandler(ShowInfo);
    filemenu.MenuItems.Add(save);
    //save.BarBreak=true;

    MenuItem exit= new MenuItem();
    exit.Text = "E&xit";
    exit.Select += new EventHandler(ShowInfo);
    filemenu.MenuItems.Add(exit);
    exit.Break=true;

Figure 3.1.8
The Break Property in use.

Each time you set the Break property, the MenuItem is placed in a new column.

Right-to-Left Menus

To cater to cultures that read right-to-left or to add an unconventional style to your menus, you can modify the menu's RightToLeft property.

1: MainMenu menu = new MainMenu();
2: menu.RightToLeft=RightToLeft.Yes;
3: MenuItem filemenu = new MenuItem();
4: filemenu.Text = "&File";

Adding line 2 to resize.cs results in the effect seen in Figure 3.1.9.

Figure 3.1.9
Right-to-left reading menus.

Comments

  1. 13 Aug 2008 at 19:50

    Actually all you have to do in place an ampersand before the letter you want underlined.

    So if you want the "r" in "Print" underlined, enter in the code:

    "P&rint"

    That's it.

  2. 15 Jul 2005 at 22:27
    this is not a programming issue and it is not something you can set unless you have a custom menu control.

    if you want to view the _ all the time for alt shortcuts you have to go to:

    display properties->appearance->effects

    and uncheck the "Hide underlined charactors for keyboard navigation until I press the ALT key" checkbox.

    these instructions are for XP SP2 Pro build 2600
  3. 15 May 2003 at 06:43

    The "" on the short-cut character is visible only when "ALT" is pressed. Is it possible to display the "" programatically???

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Introduction to Windows Forms.

Leave a comment

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!