.NET Framework Class Library
MenuItem..::.Click Event

Occurs when the menu item is clicked or selected using a shortcut key or access key defined for the menu item.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Syntax

Visual Basic (Declaration)
Public Event Click As EventHandler
Visual Basic (Usage)
Dim instance As MenuItem
Dim handler As EventHandler

AddHandler instance.Click, handler
C#
public event EventHandler Click
Visual C++
public:
 event EventHandler^ Click {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
JScript
JScript does not support events.
Remarks

The Click event occurs when this MenuItem is clicked by the user. This event also occurs if the user selects the menu item using the keyboard and presses the Enter key. It can also occur if an access key or shortcut key is pressed that is associated with the MenuItem. For more information about handling events, see Consuming Events.

NoteNote:

If the MenuItems property for the MenuItem contains any items, this event is not raised. This event is not raised for parent menu items.

Examples

The following code example demonstrates how to use the Click event to perform tasks when a MenuItem is clicked. The example creates a MainMenu called mainMenu1 and adds two MenuItem objects, topMenuItem (File) and menuItem1 (Open). It then connects the Click event to the menuItem1_Click event handler. When the user clicks the Open menu item, an OpenFileDialog is initialized and displayed. The example requires that you have created a Form named Form1.

Visual Basic
 Public Sub CreateMyMenu()
   ' Create a main menu object.
   Dim mainMenu1 As New MainMenu()

   ' Create empty menu item objects.
   Dim topMenuItem As New MenuItem()
   Dim menuItem1 As New MenuItem()

   ' Set the caption of the menu items.
   topMenuItem.Text = "&File"
   menuItem1.Text = "&Open"

   ' Add the menu items to the main menu.
   topMenuItem.MenuItems.Add(menuItem1)
   mainMenu1.MenuItems.Add(topMenuItem)

   ' Add functionality to the menu items using the Click event. 
   AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click
   ' Assign mainMenu1 to the form.
   Me.Menu = mainMenu1
End Sub


Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   ' Create a new OpenFileDialog and display it.
   Dim fd As New OpenFileDialog()
   fd.DefaultExt = "*.*"
   fd.ShowDialog()
End Sub
C#
        public void CreateMyMenu()
        {
            // Create a main menu object.
            MainMenu mainMenu1 = new MainMenu();

            // Create empty menu item objects.
            MenuItem topMenuItem = new MenuItem();
            MenuItem menuItem1 = new MenuItem();

            // Set the caption of the menu items.
            topMenuItem.Text = "&File";
            menuItem1.Text = "&Open";

            // Add the menu items to the main menu.
                 topMenuItem.MenuItems.Add(menuItem1);
            mainMenu1.MenuItems.Add(topMenuItem);
                        
            // Add functionality to the menu items using the Click event. 
            menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

            // Assign mainMenu1 to the form.
            this.Menu=mainMenu1;
        }

        private void menuItem1_Click(object sender, System.EventArgs e)
        {    
                   // Create a new OpenFileDialog and display it.
           OpenFileDialog fd = new OpenFileDialog();
                fd.DefaultExt = "*.*";
           fd.ShowDialog();
        }
Visual C++
public:
   void CreateMyMenu()
   {
      // Create a main menu object.
      MainMenu^ mainMenu1 = gcnew MainMenu;

      // Create empty menu item objects.
      MenuItem^ topMenuItem = gcnew MenuItem;
      MenuItem^ menuItem1 = gcnew MenuItem;

      // Set the caption of the menu items.
      topMenuItem->Text = "&File";
      menuItem1->Text = "&Open";

      // Add the menu items to the main menu.
      topMenuItem->MenuItems->Add( menuItem1 );
      mainMenu1->MenuItems->Add( topMenuItem );

      // Add functionality to the menu items using the Click event. 
      menuItem1->Click += gcnew System::EventHandler( this, &Form1::menuItem1_Click );

      // Assign mainMenu1 to the form.
      this->Menu = mainMenu1;
   }

private:
   void menuItem1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Create a new OpenFileDialog and display it.
      OpenFileDialog^ fd = gcnew OpenFileDialog;
      fd->DefaultExt = "*.";
      fd->ShowDialog();
   }
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
See Also

Reference

Tags :


Page view tracker