Adding a Menu

A Windows application without a menu is a rare thing. A Windows Forms application is no exception. Like the button and label you saw earlier, the menu component can be added to the Menu member of the main application, and events from the menu items can be hooked to handlers.

Menus under .NET come in two forms. MainMenu is applied to a form to provide the main user interface menu, and ContextMenu is used to respond to right mouse clicks. In both cases, the individual items within the menus are objects of type MenuItem. A menu is constructed as a hierarchy of parent and child objects. The main menu owns the individual drop-downs, which in turn own their menu items.

A typical menu creation sequence is seen in Listing 3.1.5.

Listing ...

Get C# and the .NET Framework: The C++ Perspective now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.