[[Property:title|Menu Details]]
[[Property:weight|0]]
[[Property:uuid|81ae03a0-387d-f776-17b9-f4a83c49b75f]]
Windows Forms supports menus and context menus. Main menus are displayed on a menu bar that is located immediately below the title bar of a form. The menu bar contains top-level menu items that are used to group related submenu items. For example, by clicking a '''File''' top-level menu item, you can display menu items that are related to file operations. Menu items typically appear as commands for your application (such as '''New''' and '''Open'''),but they can also appear as separator bars and submenu items. You can display a check mark next to a menu item to display the state of a command or a the state of a feature in your application. In Windows Forms, main menus are represented by the '''MainMenu''' control. 

Context menus can be displayed for a specific control or area of your form. They are typically accessed by clicking the right mouse button. In Windows Forms, context menus are represented by the '''ContextMenu''' control. 

'''ContextMenu''' and '''MainMenu''' derive from '''Menu'''. They share many properties, methods, and events. 

==Adding a MainMenu to a Form==

The following code demonstrates how to add a '''MainMenu''' to a form. 
<code>
	main_menu: WINFORMS_MAIN_MENU
</code>
<code>
	create main_menu.make
	set_menu (main_menu)
</code>

==Adding a Context Menu to a Control==

The following code demonstrates how to <span>create</span> a '''ContextMenu''' and assign it to a control. 
<code>
	label_1: WINFORMS_LABEL
	label_1_context_menu: WINFORMS_CONTEXT_MENU
</code>
<code>
	create label_1.make
	create label_1_context_menu.make
	label_1.set_context_menu (label_1_context_menu)
</code>

==Adding Menu Items==

In the following example, a '''File''' menu item is added to the '''MainMenu'''. The '''File''' menu item contains submenu items called '''Open''' and '''Exit'''. 
<code>
	mi_file: WINFORMS_MAIN_MENU
</code>
<code>
	mi_file := main_menu. get_menu_items.add (("&File").to_cil )
	dummy := mi_file. get_menu_items.add_menu_item (create {WINFORMS_MENU_ITEM}.make_from_text (("&Open...").to_cil ))
	dummy := mi_file. get_menu_items.add (("-").to_cil ) -- Gives us a separator
	dummy := mi_file. get_menu_items.add_menu_item (create  {WINFORMS_MENU_ITEM}.make_from_text(("E&xit").to_cil )
</code>

<br/>


The following code demonstrates how to handle the '''Click''' event for both the '''Open''' and '''Exit''' menu items <span>create</span>d in the previous code example. 
<code>
	mi_file := main_menu.get_menu_items.add (("&File").to_cil)
	dummy := mi_file.get_menu_items.add_menu_item (create {WINFORMS_MENU_ITEM}.make_from_text_and_on_click_and_shortcut
		(("&Open...").to_cil, create {EVENT_HANDLER}.make (Current, $FileOpen_Clicked), feature {WINFORMS_SHORTCUT}.ctrl_O))
	dummy := mi_file.get_menu_items.add (("-").to_cil) -- Gives us a separator
	dummy := mi_file.get_menu_items.add_menu_item (create {WINFORMS_MENU_ITEM}.make_from_text_and_on_click_and_shortcut
		(("E&xit").to_cil, create {EVENT_HANDLER}.make (Current, $FileExit_Clicked), feature {WINFORMS_SHORTCUT}.ctrl_X))
</code>

<br/>


The following example demonstrates how to define shortcut keys for the menu items <span>create</span>d in the previous example. 
<code>
	mi_file := main_menu.get_menu_items.add (("&File").to_cil)
	dummy := mi_file.get_menu_items.add_menu_item (create {WINFORMS_MENU_ITEM}.make_from_text_and_on_click_and_shortcut
		(("&Open...").to_cil, create {EVENT_HANDLER}.make (Current, $FileOpen_Clicked), feature {WINFORMS_SHORTCUT}.ctrl_O))
	dummy := mi_file.get_menu_items.add (("-").to_cil) -- Gives us a separator
	dummy := mi_file.get_menu_items.add_menu_item (create {WINFORMS_MENU_ITEM}.make_from_text_and_on_click_and_shortcut
		(("E&xit").to_cil, create {EVENT_HANDLER}.make (Current, $FileExit_Clicked), feature {WINFORMS_SHORTCUT}.ctrl_X))
</code>



==Adding Submenus==

The following example demonstrates how to <span>create</span> submenus. 
<code>
	mi_format: WINFORMS_MAIN_MENU
</code>
<code>
	-- Add Format Menu
	mi_format := main_menu.get_menu_items.add (("F&ormat").to_cil)

	-- Font Face sub-menu
	create mmi_sans_serif.make_from_text_and_on_click ((("").to_cil).concat_string_string (("&1.  ").to_cil,
		sans_serif_font_family.get_name), create {EVENT_HANDLER}.make (Current, $FormatFont_Clicked))
	mmi_sans_serif.set_checked (True)
	mmi_sans_serif.set_default_item (True)
	create mmi_serif.make_from_text_and_on_click ((("").to_cil).concat_string_string(("&2.  ").to_cil,
		serif_font_family.get_name), create {EVENT_HANDLER}.make (Current, $FormatFont_Clicked))
	create mmi_mono_space.make_from_text_and_on_click ((("").to_cil).concat_string_string(("&3.  ").to_cil,
	mono_space_font_family.get_name), create {EVENT_HANDLER}.make (Current, $FormatFont_Clicked))

	create l_array_menu_item.make (3)
	l_array_menu_item.put (0, mmi_sans_serif)
	l_array_menu_item.put (1, mmi_serif)
	l_array_menu_item.put (2, mmi_mono_space)
	dummy := mi_format.get_menu_items.add_string_menu_item_array (("Font &Face").to_cil, l_array_menu_item)
</code>



==Adding Default Menu Items==

The following example demonstrates how to specify a default menu item. 
<code>
	mmi_sans_serif: WINFORMS_MAIN_MENU
</code>
<code>
	create mmi_sans_serif.make_from_text_and_on_click ((("").to_cil).concat_string_string (("&1  ").to_cil,
		 sans_serif_font_family.get_name), create {EVENT_HANDLER}.make (Current, $format_font_clicked))
	mmi_sans_serif.set_checked (True)
</code>





==Adding Check Marks to Menu Items==

The following example demonstrates how to display a check mark next to a menu item. The code also demonstrates how to track which item is checked. 
<code>
	mi_medium: WINFORMS_MAIN_MENU
</code>
<code>
	create mi_medium.make_from_text_and_on_click (("&Medium").to_cil, create {EVENT_HANDLER}.make (Current, $format_size_clicked))
	mi_medium.set_checked (True)
</code>

<br/>


==Cloning Menus==

In many cases, the context menu for a control is a subset of the main menu. You cannot add the same menu items to multiple menus, but you can clone a menu item or set of menu items. The following code demonstrates how to clone the '''Format''' menu <span>create</span>d previously and add it to the context menu of a '''Label'''. 
<code>	mmy := label_1_context_menu.get_menu_items.add_menu_item (mi_format.clone_menu)
</code>






