Problem adding menuitem to context menu attached to NotifyIcon

  • Thread starter Thread starter Claire
  • Start date Start date
C

Claire

Sorry Ive added this twice (sortof) but if I'd added an addendum to the
first one then this would probably have been ignored.

This problem affects a ContextMenu attached to a NotifyIcon object. I don't
get the problem with context menus attached to list boxes etc.
Ive also seen a noticeable slow down in speed when the menu attempts to hide
after the problem occurs.

When I build a dynamic submenu on the first time round, it shows a nice list
of items.
When it gets run through on a 2nd pass, an indicator shows presence of a sub
menu but the menu never pops up.

Is this a recognized bug?

Claire
 
Remove the menuitem from the context menu.
Add child Menu items to it.
Re add the menuitem to the contextmenu.

This usually happens on controls where the contextmenu has been called by
the contextMenu.Show() method, but it looks like it also happens on
NotifyIcon.
 
Hi Jack,

The way Ive managed to solve the problem is by disposing of the parent menu
item then recreating it before adding the subitems.


Old code
========

/// Adds a submenu item for each available serial port showing current
status
private void TestComports()
{
MenuItem item;
string str;
int idx = 0;
// Clear old sub menu
mnuOpenPort.MenuItems.Clear();

// Show items in the list box
for (int nCount = 0; nCount < Comports.Count; nCount++)
{
Sax.Communications.SerialOptions Options = new
Sax.Communications.SerialOptions(SerConn.Options);
Options.PortName = Comports[nCount];
// Set the options
SerConn.SetOptions(Options);
if ((m_CommunicationManager.SerialConnected)
&& (m_CommunicationManager.SerialPortName == Comports[nCount]))
{
// add menu item saying this is the connected port
str = string.Format("{0} (Open)", (string)Comports[nCount]);
lstPorts.Items.Add(str);

//Create new menu item
item = new MenuItem(str);
item.Checked = true;
item.Click += new EventHandler(cmdConnect_Click);
//Add item to the submenu
mnuOpenPort.MenuItems.Add(item);
}
else
{
str = "";
// Test ports will open. If they open OK then close them again
try
{
SerConn.Open();
str = string.Format("{0} (Closed)", (string)Comports[nCount]);
}
catch(System.IO.IOException)
{
str = string.Format("{0} (Not available)", (string)Comports[nCount]);
}
finally
{
try
{
if (SerConn.IsOpen)SerConn.Close();
}
catch
{
}
}
// Add new item to submenu
lstPorts.Items.Add(str);
item = new MenuItem(str);
item.Click += new EventHandler(cmdConnect_Click);
mnuOpenPort.MenuItems.Add(item);
}
}
}
 
Back
Top