BUG: Dynamically Added Submenu Items are not Shown on the ContextMenu of NotifyIcon (814636)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0

SYMPTOMS

The first time that you click the menu on the ContextMenu of the notify icon, the submenu appears. However, the second time that you try this operation, the submenu does not appear. Dynamically added submenu items that are associated with menu items in a ContextMenu control of the NotifyIcon component are not displayed on the second occasion.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

WORKAROUND

To work around this issue, toggle the Visible property of the MenuItem object that the submenu is added to.

In places where a submenu item is added dynamically to the ContextMenu component of the NotifyIcon component, add the following code:
menuItem.Visible = false;
menuItem.Visible = true;
This example assumes that menuItem holds the MenuItem instance that the submenu items have been dynamically added to.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a Microsoft Visual C# Windows application in Microsoft Visual Studio .NET.
  2. From the Windows Form Toolbox, drag a TextBox control and a Button control onto the design view of the Form.
  3. From the Windows Form Toolbox, drag a ContextMenu component onto the design view of the Form.
  4. On the design view of the Form, click Context Menu.
  5. In the Type Here box, type MenuItem&1, and then press ENTER.
  6. Repeat the previous step by typing three new menuitems: MenuItem&2, MenuItem&3 , and MenuItem&4.
  7. From the Windows Form Toolbox, drag the NotifyIcon component to the design view of the Form.
  8. Right-click notifyIcon1, and then click Properties.
  9. In the Properties dialog box, click the Icon property, and then click the ellipsis button (...).
  10. In the Open dialog box, select the icon file that you want the NotifyIcon component to display on the taskbar, and then click Open.
  11. In the Properties dialog box, click the ContextMenu property, and then set the property to ContextMenu1.
  12. Double-click the button. In the event handler (button1_Click) for the button, add the following code:
    try { 
    	MenuItem subMenuItem = new MenuItem(); 
    	subMenuItem.Text = textBox1.Text; 
    	//Comment out following 2 lines code for workaround. 
    	//contextMenu1.MenuItems[0].Visible = false; 
    	//contextMenu1.MenuItems[0].Visible = true; 
    	contextMenu1.MenuItems[0].MenuItems.Add(subMenuItem); 
    } 
    catch (Exception ex) 
    { 
    	MessageBox.Show (ex.Message ); 
    }
  13. Build, and then run the application.

    The application icon that you specified for the Icon property of NotifyIcon appears on the taskbar.
  14. Type SubMenuItem&11, and then click the button on the Form.
  15. Right-click the application icon in the notification area at the far right of the taskbar. Notice that SubMenuItem11 is added to the MenuItem1 menu.
  16. Type SubMenuItem&12, and then click the button on the Form.
  17. Right-click the application icon in the notification area, and then click MenuItem1.

    Notice that the submenu that is associated with the MenuItem1 menu does not appear.

REFERENCES

For more information about NotifyIcon, see the following MSDN documentation:

Modification Type:MinorLast Reviewed:5/3/2004
Keywords:kbContMenu kbBug kbMenu kbCtrl KB814636 kbAudDeveloper