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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”