Share via


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

Note

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.

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 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.

    Note

    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
    
public NotifyIcon notifyIcon1 = new NotifyIcon();
public ContextMenu contextMenu1 = new ContextMenu();

public void 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.Visible = true;
   notifyIcon1.Text = "Right-click me!";
   notifyIcon1.Visible = true;
   notifyIcon1.ContextMenu = contextMenu1;
}
public NotifyIcon notifyIcon1 = new NotifyIcon();
public ContextMenu contextMenu1 = new ContextMenu();

public void createIconMenuStructure()
{
   // Add menu items to shortcut menu.
   contextMenu1.get_MenuItems().Add("&Open Application");
   contextMenu1.get_MenuItems().Add("S&uspend Application");
   contextMenu1.get_MenuItems().Add("E&xit");

   // Set properties of NotifyIcon component.
   notifyIcon1.set_Icon(new System.Drawing.Icon
      (System.Environment.GetFolderPath
      (System.Environment.SpecialFolder.Personal)
      + "\\Icon.ico"));
   notifyIcon1.set_Text("Right-click me!");
   notifyIcon1.set_Visible(true);
   notifyIcon1.set_ContextMenu(contextMenu1);
}
public:
   System::Windows::Forms::NotifyIcon ^ notifyIcon1;
   System::Windows::Forms::ContextMenu ^ contextMenu1;

   void 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 = gcnew System::Drawing::Icon
          (String::Concat(System::Environment::GetFolderPath
          (System::Environment::SpecialFolder::Personal),
          "\\Icon.ico"));
      notifyIcon1->Text = "Right-click me!";
      notifyIcon1->Visible = true;
      notifyIcon1->ContextMenu = contextMenu1;
   }

Note

You must initialize notifyIcon1 and contextMenu1, which you can do by including the following statements in the constructor of your form:

notifyIcon1 = gcnew System::Windows::Forms::NotifyIcon();
contextMenu1 = gcnew System::Windows::Forms::ContextMenu();

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)