Compartilhar via


How to: Associate a Shortcut Menu with a Windows Forms NotifyIcon Component

NoteNote

Although MenuStrip and ContextMenuStrip replace and add functionality to the MainMenu and ContextMenu controls of previous versions, MainMenu and ContextMenu are retained for both backward compatibility and future use if you choose.

The NotifyIcon component displays an icon in the status notification area of the taskbar. Commonly, applications enable you to right-click this icon to send commands to the application it represents. By associating a ContextMenu component with the NotifyIcon component, you can add this functionality to your applications.

NoteNote

If you want your application to be minimized at startup while displaying an instance of the NotifyIcon component in the taskbar, set the main form's WindowState property to Minimized and be sure the NotifyIcon component's Visible property is set to true.

To associate a shortcut menu with the NotifyIcon component at design time

  1. Add a NotifyIcon component to your form, and set the important properties, such as the Icon and Visible properties.

    For more information, see How to: Add Application Icons to the TaskBar with the Windows Forms NotifyIcon Component.

  2. Add a ContextMenu component to your Windows Form.

    Add menu items to the shortcut menu representing the commands you want to make available at run time. This is also a good time to add menu enhancements to these menu items, such as access keys.

  3. Set the ContextMenu property of the NotifyIcon component to the shortcut menu that you added.

    With this property set, the shortcut menu will be displayed when the icon on the taskbar is clicked.

To associate a shortcut menu with the NotifyIcon component programmatically

  1. Create an instance of the NotifyIcon class and a ContextMenu class, with whatever property settings are necessary for the application (Icon and Visible properties for the NotifyIcon component, menu items for the ContextMenu component).

  2. Set the ContextMenu property of the NotifyIcon component to the shortcut menu that you added.

    With this property set, the shortcut menu will be displayed when the icon on the taskbar is clicked.

    NoteNote

    The following code example creates a basic menu structure. You will need to customize the menu choices to those that fit the application you are developing. Additionally, you will want to write code to handle the Click events for these menu items.

    Public ContextMenu1 As New ContextMenu
    Public NotifyIcon1 As New NotifyIcon
    
    Public Sub CreateIconMenuStructure()
       ' Add menu items to shortcut menu.
       ContextMenu1.MenuItems.Add("&Open Application")
       ContextMenu1.MenuItems.Add("S&uspend Application")
       ContextMenu1.MenuItems.Add("E&xit")
    
       ' Set properties of NotifyIcon component.
       NotifyIcon1.Icon = New System.Drawing.Icon _ 
          (System.Environment.GetFolderPath _ 
          (System.Environment.SpecialFolder.Personal)  _ 
          & "\Icon.ico")
       NotifyIcon1.Text = "Right-click me!"
       NotifyIcon1.Visible = True
       NotifyIcon1.ContextMenu = ContextMenu1
    End Sub
    

See Also

Tasks

How to: Add Application Icons to the TaskBar with the Windows Forms NotifyIcon Component

Reference

NotifyIcon Component Overview (Windows Forms)
NotifyIcon
Icon

Other Resources

NotifyIcon Component (Windows Forms)