Menu Details

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. main_menu: WINFORMS_MAIN_MENU create main_menu.make set_menu (main_menu)

Adding a Context Menu to a Control

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

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. mi_file: WINFORMS_MAIN_MENU 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 )


The following code demonstrates how to handle the Click event for both the Open and Exit menu items created in the previous code example. 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))


The following example demonstrates how to define shortcut keys for the menu items created in the previous example. 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))

Adding Submenus

The following example demonstrates how to create submenus. mi_format: WINFORMS_MAIN_MENU -- 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)

Adding Default Menu Items

The following example demonstrates how to specify a default menu item. mmi_sans_serif: WINFORMS_MAIN_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, $format_font_clicked)) mmi_sans_serif.set_checked (True)

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. mi_medium: WINFORMS_MAIN_MENU 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)


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 created previously and add it to the context menu of a Label. mmy := label_1_context_menu.get_menu_items.add_menu_item (mi_format.clone_menu)

cached: 12/21/2024 12:07:15.000 PM