Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Book Cover C# and the .NET Framework
63577 times
Rated
Read 63,577 times

Contents

Related Categories

Introduction to Windows Forms - Menu Layout

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

  • Re: [1770] Introduction to Windows Forms

    Posted by Dracorat on 13 Aug 2008

    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 ...

  • short-cuts

    Posted by rholden on 15 Jul 2005

    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 pro...

  • Sort-cuts

    Posted by DotDot on 15 May 2003

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