Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
ContextMenu Problem
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.windowsforms.


Fredrik
GOOD ANSWER
Hi,

I have the following problem.
I'm trying to create a dynamic ContextMenu (used by a NotifyIcon), but when I repopulate the submenus the submenus won't show. the little arrow is there - indicating that there are submenues available - but they won't popup.

On the ContextMenu objects Popup eventhandler i have the routine that populates/repopulates the submenus.
The submenues is populated like this:

menuConnect.MenuItems.Clear();
menuDisconnect.MenuItems.Clear();
string[] names = reg.GetSubKeyNames();
for (int i=0; i<names.Length; i++) {
    if (names[i].ToLower().Equals("startup") || names[i].ToLower().Equals("")) continue;
    menuConnect.MenuItems.Add(new MenuItem(names[i], new EventHandler(menuConnect_Click)));
    menuDisconnect.MenuItems.Add(new MenuItem(names[i], new EventHandler(menuDisconnect_Click)));
}

where menuContent and menuDisconnect are menuitems added to the contextMenu in design-time.
Any help is appriciated. I've run out of ideas on how to solve this.

The menu "looks" like this:
Exit
Settings
------ (break)
Connect
- Submenu to Connect here
Disconnect
- Submenu to Disconnect here

// Fredrik
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Mike in Paradise
GOOD ANSWER
You are trying to assign the same menu item name to both connect and disconnect.. See your code modified in this pop up routine

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ContextMEnuOnFly
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuConnect;
private System.Windows.Forms.MenuItem menuDisconnect;
private System.Windows.Forms.Label label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
    if( disposing )
    {
        if (components != null)
        {
            components.Dispose();
        }
    }
    base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.contextMenu1 = new System.Windows.Forms.ContextMenu();
    this.menuItem1 = new System.Windows.Forms.MenuItem();
    this.menuItem2 = new System.Windows.Forms.MenuItem();
    this.menuConnect = new System.Windows.Forms.MenuItem();
    this.menuDisconnect = new System.Windows.Forms.MenuItem();
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    //
    // contextMenu1
    //
    this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                    this.menuItem1,
                                                                                    this.menuItem2,
                                                                                    this.menuConnect,
                                                                                    this.menuDisconnect});
    this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup);
    //
    // menuItem1
    //
    this.menuItem1.Index = 0;
    this.menuItem1.Text = "Exit";
    //
    // menuItem2
    //
    this.menuItem2.Index = 1;
    this.menuItem2.Text = "-";
    //
    // menuConnect
    //
    this.menuConnect.Index = 2;
    this.menuConnect.Text = "Connect";
    //
    // menuDisconnect
    //
    this.menuDisconnect.Index = 3;
    this.menuDisconnect.Text = "Disconnect";
    //
    // label1
    //
    this.label1.Location = new System.Drawing.Point(64, 56);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(100, 72);
    this.label1.TabIndex = 0;
    this.label1.Text = "Right Click for your menu...";
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.ContextMenu = this.contextMenu1;
    this.Controls.Add(this.label1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    Application.Run(new Form1());
}
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
    menuConnect.MenuItems.Clear();
    menuDisconnect.MenuItems.Clear();
    string[] names = new String[] {"Option1","Option2","Option3"};
    for (int i=0; i<names.Length; i++)
    {
        if (names[i].ToLower().Equals("startup") || names[i].ToLower().Equals("")) continue;
        menuConnect.MenuItems.Add(new MenuItem(names[i]+"C", new EventHandler(menuConnect_Click)));
        menuDisconnect.MenuItems.Add(new MenuItem(names[i]+"D", new EventHandler(menuDisconnect_Click)));
    }
}
private void menuConnect_Click(object sender, EventArgs e)
{
    this.label1.Text = ((MenuItem)sender).Text;
}
private void menuDisconnect_Click(object sender, EventArgs e)
{
    this.label1.Text = ((MenuItem)sender).Text;
}
}
}
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Fredrik
GOOD ANSWER
Thanks for your reply, but it didn't make any difference.
It will show the menu after the first population, but when I've cleared the menu and repopulated it it will not show again.

// Fredrik
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
PocketNerd
GOOD ANSWER
Hi Fredrik,

Try deleting the top level menu items and then add them again so, in your menu structure ...

The menu "looks" like this:
Exit
Settings
------ (break)
Connect
- Submenu to Connect here
Disconnect
- Submenu to Disconnect here

1) Delete the Connect and Disconnect menu items
2) Create new Menu Items for Connect and Disconnect
3) Add the sub-menu items to the Connect and Disconnect
4) Add the Connect and Disconnect menu items back to the main menu.

e.g.

menuConnect.MenuItems.Clear();
menuDisconnect.MenuItems.Clear();

// Remove menuConnect
// Remove menuDisconnect

// Create new menuConnect
menuConnect = new MenuItem( "Connect" ); // etc

// Create new menuConnect
menuDisconnect = new MenuItem( "Disconnect" ); // etc

string[] names = reg.GetSubKeyNames();
for (int i=0; i<names.Length; i++) {
    if (names[i].ToLower().Equals("startup") || names[i].ToLower().Equals("")) continue;
    menuConnect.MenuItems.Add(new MenuItem(names[i], new EventHandler(menuConnect_Click)));
    menuDisconnect.MenuItems.Add(new MenuItem(names[i], new EventHandler(menuDisconnect_Click)));

I am not sure why but i had exactly the same problem and this worked fine :s

HTH

RS
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Fredrik
GOOD ANSWER
Thanks, that worked.

But can someone explain why it has to be done this way?

// Fredrik
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Igal Nassi
GOOD ANSWER
Yes please. I would appreciate an explaination on this too!

I am beginning to get fed up with these silly problems that take ages to find out. Why do people in Microsoft don't have a clear bug database where you can browse all the classes and see the endless bugs!

Regards...

--------------------------------
From: Igal Nassi
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
M M
GOOD ANSWER
(Type your message here)

--------------------------------
From: M M

Hi, I'm experience the same problem. It is a known microsoft problem. There is an easier workaround.

You need to toggle the visibility property (of the menu to which you are adding a new one to) to false and then true.

For more info, go to msdn.microsoft.com and search for the following article:

Microsoft Knowledge Base Article - 814636
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Paul McEwan
GOOD ANSWER
You don't actually have to remove and add the menu items.
Simply make set the parent menu's visible property to false and then back to true again. This seems to refresh it.

I wonder if anyone's reported this to Microsoft?

--------------------------------
From: Paul McEwan
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
    
Jon Davidson
GOOD ANSWER
The problem is a BUG in Microsoft's .NET implementation for ContextMenus and NotifyIcons. I created a workaround for it.

visit my .plan file for 4/6/2005 for the fix.

http://www.esoomsoftware.com/planview.aspx?alias=Chaos&dotpi=0

--------------------------------
From: Jon Davidson
Reply to this message...
Vote that this is a GOOD answer... (2 votes from other users already)
 
 
 
System.ComponentModel.Container
System.Drawing.Point
System.Drawing.Size
System.EventArgs
System.EventHandler
System.String
System.Windows.Forms.Application
System.Windows.Forms.ContextMenu
System.Windows.Forms.Form
System.Windows.Forms.Label
System.Windows.Forms.Menu
System.Windows.Forms.MenuItem
System.Windows.Forms.NotifyIcon




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734