Export (0) Print
Expand All

Associating a Context Menu with a Windows Forms NotifyIcon Component

Visual Studio .NET 2003

The Windows Forms NotifyIcon component displays an icon in the status notification area of the taskbar. Commonly, applications allow 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.

Note   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 context 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 Adding Application Icons to the TaskBar with the Windows Forms NotifyIcon Component.
  2. Add a ContextMenu component to your Windows Form. For more information, see Adding Context Menus to Windows Forms.

    Add menu items to the context menu representing the commands you wish to make available at run time. This is also a good time to add menu enhancements to these menu items, such as access keys. For more information, see Adding Menu Enhancements to Windows Forms.

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

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

To associate a context menu with the NotifyIcon component programmatically

  1. Instantiate a NotifyIcon component and a ContextMenu component, 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 context menu that you added.

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

    Note   The following code 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. For more information, see MenuItem.Click.
    ' Visual Basic
    Public ContextMenu1 As New ContextMenu
    Public NotifyIcon1 As New NotifyIcon
    
    Public Sub CreateIconMenuStructure()
       ' Add menu items to context menu.
       ContextMenu1.MenuItems.Add("&Open Application")
       ContextMenu1.MenuItems.Add("S&uspend Application")
       ContextMenu1.MenuItems.Add("E&xit")
    
       ' Set properties of NotifyIcon component.
       NotifyIcon1.Visible = True
       NotifyIcon1.Icon = New System.Drawing.Icon _ 
          (System.Environment.GetFolderPath _ 
          (System.Environment.SpecialFolder.Personal)  _ 
          & "\Icon.ico")
       NotifyIcon1.Text = "Right-click me!"
       NotifyIcon1.ContextMenu = ContextMenu1
    End Sub
    
    // C#
    public NotifyIcon notifyIcon1 = new NotifyIcon();
    public ContextMenu contextMenu1 = new ContextMenu();
    
    public void createIconMenuStructure()
    {
       // Add menu items to context menu.
       contextMenu1.MenuItems.Add("&Open Application");
       contextMenu1.MenuItems.Add("S&uspend Application");
       contextMenu1.MenuItems.Add("E&xit");
    
       // Set properties of NotifyIcon component.
       notifyIcon1.Visible = true;
       notifyIcon1.Icon = new System.Drawing.Icon
          (System.Environment.GetFolderPath
          (System.Environment.SpecialFolder.Personal)
          + @"\Icon.ico");
       notifyIcon1.Text = "Right-click me!";
       notifyIcon1.ContextMenu = contextMenu1;
    }
    
    // C++
    public:
       System::Windows::Forms::NotifyIcon * notifyIcon1;
       System::Windows::Forms::ContextMenu * contextMenu1;
    
       void createIconMenuStructure()
       {
          // Add menu items to context menu.
          contextMenu1->MenuItems->Add("&Open Application");
          contextMenu1->MenuItems->Add("S&uspend Application");
          contextMenu1->MenuItems->Add("E&xit");
    
          // Set properties of NotifyIcon component.
          notifyIcon1->Visible = true;
          notifyIcon1->Icon = new System::Drawing::Icon
              (String::Concat(System::Environment::GetFolderPath
              (System::Environment::SpecialFolder::Personal),
              "\\Icon.ico"));
          notifyIcon1->Text = "Right-click me!";
          notifyIcon1->ContextMenu = contextMenu1;
       }
    
    Visual C++ Note   You must initialize
    notifyIcon1
    and
    contextMenu1,
    which you can do by including the following statements in the constructor of your form:
    // C++
    notifyIcon1 = new System::Windows::Forms::NotifyIcon();
    contextMenu1 = new System::Windows::Forms::ContextMenu();

See Also

Adding Application Icons to the TaskBar with the Windows Forms NotifyIcon Component | NotifyIcon Component (Windows Forms) | Introduction to the Windows Forms NotifyIcon Component | NotifyIcon Class | NotifyIcon.Icon Property

Show:
© 2014 Microsoft