An imperative part of the user interface in a Windows-based application
is the menu. In this article I elucidate how to adding menus and
menuitem to Windows forms, Replacing, Cloning, Merging of menus
and about Context menus (Popupmenus).
A menu on a form is created with a MainMenu object, which is a collection
of MenuItem objects. You can add menus to Windows Forms at design
time by adding the MainMenu control and then adding menu items to
it using the Menu Designer. Menus can also be added programmatically
by adding one or more MainMenu controls to a form and adding MenuItem
objects to the collection.
Now we perceive the following information regarding menus with instances.
Adding Menu, Menu Items to a Menu.
Adding Menu Enhancements to Windows Forms Menus.
Replacing, Cloning, and Merging of Menus.
Context menus (Popupmenus).
1. Adding Menu, Menu Items to a Menu
First add a MainMenu control to the form. Then to add menu items
to it add MenuItem objects to the collection. By default, a MainMenu
object contains no menu items, so that the first menu item added
becomes the menu heading. Menu items can also be dynamically added
when they are created, such that properties are set at the time
of their creation and addition.
The following program shows how to create a simple menu.
The Class MenuTest1 creates a simple menu on a form. The form has
a top-level
File menu with menu items New,Open and Exit .The menu also includes
a About menu.
using System;
using System.ComponentModel;
using System.WinForms;
using System.Drawing;
public class MenuTest1 :Form {
private MainMenu mainMenu;
public MenuTest1() {
mainMenu = new MainMenu();
MenuItem File = mainMenu.MenuItems.Add("&File");
File .MenuItems.Add(new MenuItem("&New"));
File .MenuItems.Add(new MenuItem("&Open"));
File .MenuItems.Add(new MenuItem("&Exit"));
this.Menu=mainMenu;
MenuItem About = mainMenu.MenuItems.Add("&About");
About.MenuItems.Add(new MenuItem("&About"));
this.Menu=mainMenu;
mainMenu.GetForm().BackColor = Color.Indigo ;
Now in this point let us see some of the Members, properties of
the Menu type. Some Members:
CloneMenu () -- Sets this menu to be a similar copy
of another menu.
MergeMenu () -- Merges an additional menu's items with
this one.
There are two properties named mergeType and mergeOrder
mergeType -- Determine how individual menu items
are handled during a menu merge and the relative location
of each Menu Item in the newly merged menu.
mergeOrder -- Determine the menu items' presence
and location within a menu merge.
GetMainMenu () -- Returns the Main Menu item that holds
this menu.
MdiListItem -- This property returns the MenuItem that
contains the list of MDI child windows.
Some properties:
Index -- Gets or sets the menu item's position in
its parent menu.
Checked -- Gets or sets a value representing whether
a check mark appears nearby the text of the menu item.
Text -- Gets or sets the text of the menu item.
Shortcut -- Gets or sets the shortcut key connected
with the menu item.
Enabled -- Gets or sets a value representing whether
the menu item is enabled.
To get information about other members and properties open the System.WinForms.dll
with the help of ILDASM.exe as shown in the figures below.
2. Adding Menu Enhancements to Windows Forms Menus
There are four enhancements that can be added to menus to convey
information to users.
Check marks can be used to designate whether a feature is
turned on or off.
Shortcut keys are keyboard commands to access menu items within
an application.
To add an access key to a menu item.
Separator bars are used to group related commands within a
menu and make menus easier to read.
Now let us observe those things in details. I enlighten the usage
of check marks in specify in Context menus. Shortcut keys are widely
used to access menu items.
So by pressing the function key F1 we can access the menu Item function.
To add an access key to a menu item just simply put ampersand (&)
prior to the letter you want to be underlined as the access key.
Make the text property of menu item a dash (-) to make that menu
item a separator bar. In the following program I also included the
Event handlers to all the menu items.
The following program shows how to create a menu with the enhancements.
using
System;
using System.ComponentModel;
using System.WinForms;
using System.Drawing;
At times, when creating Windows Forms applications, we require generating
a copy of a menu that already created. In such circumstances, rather
than recreating complete menu structures when you want to duplicate
a given menu's functionality, we can use the CloneMenu method.
Likewise it is useful to have the items from two menus appear in
a single menu, such as merging a Multiple Document Interface (MDI)
container's menu with that of its active MDI child. Similarly in
some situations depending on the run-time requirements of the application
we have to Add, Delete the menus or Enabling, disabling of the menu
items.
Now let us see all those things in details with an example.
In the following program it has a top-level File menu with menu
items Merge (Edit-View), Remove (Merged), Disable (View), Enable
(View), Replace (Edit-View). The menu also includes a View menu
and Edit menu. If we click the Merge (Edit-View) menu item then
it creates a new top-level menu named "Merged" by merging
the edit menu and view menu.
If we click the Remove (Merged) menu item then it removes the newly
created "Merged" menu. If we click the Disable (View)
menu item then it disables the menu items of View menu. It is useful
to limit or broaden the commands a user may make. If we click the
Enable (View) menu item then it enables the menu items of View menu.
If we click the Replace (Edit-View) menu item then it replaces the
position of the Edit and View menus.
The Example:
using
System;
using System.ComponentModel;
using System.WinForms;
using System.Drawing;
public class MenuTest :Form {
private MainMenu mainMenu;
MenuItem Editmen,Viewmen;
Context menus are used inside applications to provide users access
to often used commands by means of a right-click of the mouse. Often,
context menus are assigned to controls, and provide particular commands
that relate to that precise control.
The Example:
using
System;
using System.ComponentModel;
using System.WinForms;
using System.Drawing;
public class MainForm :Form {
private ContextMenu popUpMenu;
private MenuItem Currentcheck;
private MenuItem checkBold;
private MenuItem checkItalic;
private MenuItem checkUnderline;
public MainForm() {
popUpMenu =new ContextMenu();
popUpMenu.MenuItems.Add("Bold",new EventHandler(popup));
popUpMenu.MenuItems.Add("Italic",new EventHandler(popup));
popUpMenu.MenuItems.Add("Underline",new EventHandler(popup));
this.ContextMenu = popUpMenu;
this.BackColor = Color.Sienna ;
checkBold=this.ContextMenu.MenuItems[0];
checkItalic=this.ContextMenu.MenuItems[1];
checkUnderline=this.ContextMenu.MenuItems[2];
Currentcheck=checkBold;
Currentcheck.Checked=true;
}
G. Gnana Arun Ganesh is the Administrator and the Founder of ARUN
MICRO SYSTEMS (www.arunmicrosystems.netfirms.com).
He has been programming in C++, Visual Basic, COM, Java and Microsoft
Technologies for 4 years. He has written over 50 articles on .NET
published in the top C# websites such as CsharpCorner, CsharpHelp,
MSDNAA, Code Project, Developersdex, 411asp.net, ProgrammersHeaven,
VB-World, and Devguru etc. He has performed numerous technical reviews
for Prentice Hall, Prentice Hall PTR and Sams. He has also created
real time projects in Web Services. Currently reviewing a book on
.NET Security and put forward a couple of proposals on .NET and
in C#. You can contact him for technological support and queries
through ggarung@rediffmail.com