Microsoft KB Archive/814636

= BUG: Dynamically Added Submenu Items are not Shown on the ContextMenu of NotifyIcon =

Article ID: 814636

Article Last Modified on 5/16/2007

-

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.



Steps to Reproduce the Behavior
 Create a Microsoft Visual C# Windows application in Microsoft Visual Studio .NET. From the Windows Form Toolbox, drag a TextBox control and a Button control onto the design view of the Form. From the Windows Form Toolbox, drag a ContextMenu component onto the design view of the Form. On the design view of the Form, click Context Menu. In the Type Here box, type MenuItem&1, and then press ENTER. Repeat the previous step by typing three new menuitems: MenuItem&2, MenuItem&3 , and MenuItem&4 .</li> From the Windows Form Toolbox, drag the NotifyIcon component to the design view of the Form.</li> Right-click notifyIcon1, and then click Properties.</li> In the Properties dialog box, click the Icon property, and then click the ellipsis button (...).</li> In the Open dialog box, select the icon file that you want the NotifyIcon component to display on the taskbar, and then click Open.</li> In the Properties dialog box, click the ContextMenu property, and then set the property to ContextMenu1.</li>  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 ); } </li> Build, and then run the application.

The application icon that you specified for the Icon property of NotifyIcon appears on the taskbar.</li> Type SubMenuItem&11, and then click the button on the Form.</li> 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.</li> Type SubMenuItem&12, and then click the button on the Form.</li> 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.</li></ol>

<div class="references_section">