Adding a menu PDF Print E-mail

Menus are used by most stand alone Windows applications. They can be used in a Windows Forms application as follows:

  1.  
  2. using System;
  3. using System.Windows.Forms;
  4. class MenuSample : Form {
  5. public static void Main() {
  6. Application.Run( new MenuSample() );
  7. }
  8. MenuSample() {
  9. MenuItem mi = new MenuItem();
  10. mi.Text = "A Menu Item";
  11. mi.Click += new EventHandler( clickHandler );
  12. Menu = new MainMenu();
  13. Menu.MenuItems.Add( mi );
  14. }
  15. void clickHandler( object o, EventArgs ea ) {
  16. MessageBox.Show( "You have clicked the menu item" );
  17. }
  18. }
 

Just like the Button sample, we declare a MenuItem objet, label it and add an event handler. Then we assign a new MainMenu object to the Menu property of the form and we add the menu item to the collection of MenuItems maintained by the MainMenu object using the Add method of the collection class.

The handler is identical to the one used in the Button sample.




Bookmark it...
Digg!Reddit!Del.icio.us!Google!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!
 
< Prev   Next >
Joomla Template by Joomlashack
Joomla Templates by JoomlaShack Joomla Templates by Compass Design